首页 > 其他 > 详细

selenium 打开浏览器

时间:2017-01-03 16:25:57      阅读:144      评论:0      收藏:0      [点我收藏+]
 1 import org.openqa.selenium.By;
 2 import org.openqa.selenium.WebDriver;
 3 import org.openqa.selenium.WebElement;
 4 import org.openqa.selenium.firefox.FirefoxDriver;
 5 import org.openqa.selenium.firefox.FirefoxProfile;
 6 import org.openqa.selenium.ie.InternetExplorerDriver;
 7 import org.openqa.selenium.support.ui.ExpectedCondition;
 8 import org.openqa.selenium.support.ui.WebDriverWait;
 9 // 如果你的 FireFox 没有安装在默认目录,那么必须在程序中设置
10         // System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
11         // 创建一个 FireFox 的浏览器实例
12         WebDriver driver = new FirefoxDriver();
13 
14         // 让浏览器访问 Baidu
15         driver.get("http://www.baidu.com");
16         // 用下面代码也可以实现
17         // driver.navigate().to("http://www.baidu.com");
18 
19         // 获取 网页的 title
20         System.out.println("1 Page title is: " + driver.getTitle());
21 
22         // 通过 id 找到 input 的 DOM
23         WebElement element = driver.findElement(By.id("kw"));
24 
25         // 输入关键字
26         element.sendKeys("ZTree");
27 
28         // 提交 input 所在的  form
29         element.submit();
30         
31         // 通过判断 title 内容等待搜索页面加载完毕,Timeout 设置10秒
32         (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
33             public Boolean apply(WebDriver d) {
34                 return d.getTitle().toLowerCase().endsWith("Ztree");
35             }
36         });
37 
38         // 显示搜索结果页面的 title
39         System.out.println("2 Page title is: " + driver.getTitle());
40         
41         //关闭浏览器
42         driver.quit();
43         
44 /*         WebDriver driver = new FirefoxDriver();  
45          
46             Navigation navigation = driver.navigate();  
47             navigation.to("https://www.baidu.com");  
48               
49             WebElement subox = driver.findElement(By.id("kw"));  
50             subox.sendKeys("景甜长城剧照");  
51             WebElement subtn = driver.findElement(By.id("su"));  
52             subtn.click();  
53       
54             try {  
55             Thread.sleep(3000);  
56             } catch (InterruptedException e) {  
57                 e.printStackTrace();  
58             }  
59       
60             driver.close();  
61         */

 

selenium 打开浏览器

原文:http://www.cnblogs.com/Lxiaojiang/p/6245165.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!