selenium 代理 Cookies 截图 等待 调用JS
1. 改变用户代理
- import org.junit.AfterClass;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.firefox.FirefoxDriver;
- import org.openqa.selenium.firefox.FirefoxProfile;
- public class ProxyTest {
- static WebDriver driver;
- @BeforeClass
- public static void beforeClass(){
- //代理的IP和端口
- String proxyIP="192.168.12.0";
- int proxyPort=80;
- FirefoxProfile profile=new FirefoxProfile();
- //使用代理
- profile.setPreference("network.proxy.type", 1);
- //配置“HTTP代理:(N)”
- profile.setPreference("network.proxy.http", proxyIP);
- profile.setPreference("network.proxy.http_prot", proxyPort);
- //选中 “为所有协议使用相同代理(S)” 选框
- profile.setPreference("network.proxy.share_proxy_settings", true);
- //配置“不使用代理:(N)”文本框
- profile.setPreference("network.proxy.no_proxies_on", "localhost, 127.0.0.1");
- //以代理的方式启动火狐
- driver=new FirefoxDriver(profile);
- }
- @Test
- public void test() {
- driver.get("http://www.baidu.com");
- }
- @AfterClass
- public static void afterClass(){
- //driver.quit();
- }
- }
运行之后,在打开的火狐上查看代理设置(选项->高级->网络->连接->设置),显示如下:
友情提示:测试完之后一定要改回来。默认是“使用系统代理设置”。
2.读取Cookies
增加cookie:
Cookie cookie = new Cookie("key", "value");
driver.manage().addCookie(cookie);
获取cookie的值:
Set<Cookie> allCookies = driver.manage().getCookies();
根据某个cookie的name获取cookie的值:
driver.manage().getCookieNamed("cookieName");
删除cookie:
driver.manage().deleteCookieNamed("CookieName");
driver.manage().deleteCookie(loadedCookie);
driver.manage().deleteAllCookies();
下面是一个例子:
- import java.util.Set;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- import org.openqa.selenium.Cookie;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.firefox.FirefoxDriver;
- public class CookieTest {
- WebDriver driver;
- @Before
- public void setUp() throws Exception {
- driver=new FirefoxDriver();
- }
- @After
- public void tearDown() throws Exception {
- driver.quit();
- }
- @Test
- public void test() {
- printCookie();
- //添加一个cookie
- Cookie cookie=new Cookie("s","selenium");
- driver.manage().addCookie(cookie);
- printCookie();
- //删除一个cookie
- driver.manage().deleteCookie(cookie); //driver.manage().deleteCookieNamed("s");也可以
- printCookie();
- }
- public void printCookie(){
- //获取并打印所有的cookie
- Set<Cookie> allCookies=driver.manage().getCookies();
- System.out.println("----------Begin---------------");
- for(Cookie c:allCookies){
- System.out.println(String.format("%s->%s",c.getName(),c.getValue()));
- }
- System.out.println("----------End---------------");
- }
- }
运行结果显示为:
----------Begin---------------
----------End---------------
----------Begin---------------
s->selenium
----------End---------------
----------Begin---------------
----------End---------------
可以看到添加cookie之前,系统的cookie为空。之后成功添加了cookie(s,selenium)并且最后成功删除。
3. 调用Java Script
selenium提供了executeScript
、executeAsyncScript
两个方法来处理js调用的问题。其中executeScript
是同步方法,用它执行js代码会阻塞主线程执行,直到js代码执行完毕;executeAsyncScript
方法是异步方法,它不会阻塞主线程执行。
- import org.junit.Test;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.firefox.FirefoxDriver;
- import org.openqa.selenium.JavascriptExecutor;
- public class JSTest {
- @Test
- public void test() {
- WebDriver driver=new FirefoxDriver();
- driver.get("http://www.baidu.com");
- JavascriptExecutor js=(JavascriptExecutor)driver;
- js.executeScript("document.getElementById(\"kw\").value=\"selenium\"");
- //利用js代码获取百度搜索框内文字
- String keyword = (String) (js.executeScript("var input = document.getElementById(\"kw\").value;return input"));
- System.out.println(keyword);
- driver.quit();
- }
- }
运行结果:
selenium
4. Webdriver截图
- import java.io.File;
- import java.io.IOException;
- import org.apache.commons.io.FileUtils;
- import org.junit.Test;
- import org.openqa.selenium.OutputType;
- import org.openqa.selenium.TakesScreenshot;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.firefox.FirefoxDriver;
- public class ScreenShotTest {
- @Test
- public void test() {
- WebDriver driver=new FirefoxDriver();
- driver.get("http://www.baidu.com");
- screenShot("selenium.jpg", (TakesScreenshot)driver);
- driver.quit();
- }
- public static void screenShot(String name,TakesScreenshot driver){
- String savaPath=System.getProperty("user.dir");
- File sourceFile=driver.getScreenshotAs(OutputType.FILE);
- try{
- System.out.println("Begin saving screenshot to path:"+savaPath);
- FileUtils.copyFile(sourceFile, new File(savaPath+"\\"+name));
- } catch(IOException e){
- System.out.println("Save screenshot failed!");
- e.printStackTrace();
- } finally{
- System.out.println("Finish screenshot!");
- }
- }
- }
运行结果:
Begin saving screenshot to path:D:\workspace_luna\SeleniumDemo
Finish screenshot!
由于eclipse的workspace不一样,会导致上面的路径不一样。不过没关系,我们打开对应的路径就可以看到selenium.jpg。打开以后是百度首页的截图。
5. 页面等待
因为Load页面需要一段时间,如果页面还没加载完就查找元素,必然是查找不到的。最好的方式,就是设置一个默认等待时间,在查找页面元素的时候如果找不到就等待一段时间再找,直到超时。Webdriver提供两种方法,一种是显性等待,另一种是隐性等待。
显性等待:
显示等待就是明确的要等到某个元素的出现或者是某个元素的可点击等条件,等不到,就一直等,除非在规定的时间之内都没找到,那么就跳出Exception.
- new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.id("kw")));
当然也可以自己重写内部类:
- WebElement e = (new WebDriverWait( driver, 10)).until(
- new ExpectedCondition< WebElement>(){
- @Override
- public WebElement apply( WebDriver d) {
- return d.findElement(By.linkText("Selenium_百度百科"));
- }
- });
下面的例子是:打开百度,搜索“selenium",等待搜索结果里出现“Selenium_百度百科”的时候点击该链接。
- import org.junit.Test;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.firefox.FirefoxDriver;
- import org.openqa.selenium.support.ui.ExpectedCondition;
- import org.openqa.selenium.support.ui.ExpectedConditions;
- import org.openqa.selenium.support.ui.WebDriverWait;
- public class ExplicitWaitTest {
- @Test
- public void test() {
- WebDriver driver=new FirefoxDriver();
- driver.get("http://www.baidu.com");
- driver.findElement(By.id("kw")).sendKeys("selenium");
- driver.findElement(By.id("su")).click();
- //方法一
- //new WebDriverWait(driver,10).until(ExpectedConditions.presenceOfElementLocated(By.linkText("Selenium_百度百科")));
- //driver.findElement(By.linkText("Selenium_百度百科")).click();
- //方法二
- WebElement e = (new WebDriverWait( driver, 10)).until(
- new ExpectedCondition< WebElement>(){
- @Override
- public WebElement apply( WebDriver d) {
- return d.findElement(By.linkText("Selenium_百度百科"));
- }
- }
- );
- e.click();
- driver.quit();
- }
- }
运行的话是通过的,但是如果中间不显示等待的话,直接查找就会fail。
隐性等待:
隐形等待只是等待一段时间,元素不一定要出现,只要时间到了就会继续执行。
- driver.manage().timeouts().implicitlyWait(second, TimeUnit.SECONDS);
还是上面的例子,用隐性等待写就是:
- import java.util.concurrent.TimeUnit;
- import org.junit.Test;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.firefox.FirefoxDriver;
- public class ImplicitWaitTest {
- @Test
- public void test() {
- WebDriver driver=new FirefoxDriver();
- driver.get("http://www.baidu.com");
- driver.findElement(By.id("kw")).sendKeys("selenium");
- driver.findElement(By.id("su")).click();
- driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
- driver.findElement(By.linkText("Selenium_百度百科")).click();
- driver.quit();
- }
- }
运行结果还是通过的。不过个人觉得看起来跟笨方法”Thread.sleep()"差不多,只不多比它更高效了而已。
selenium 代理 Cookies 截图 等待 调用JS的更多相关文章
- python之selenium调用js(execute_script)
转载: http://www.cnblogs.com/fnng/p/3230768.html 本节重点: 调用js方法 execute_script(script, *args) 在当前窗口/框架 同 ...
- python selenium --调用js
转自:http://www.cnblogs.com/fnng/p/3230768.html 本节重点: 调用js方法 execute_script(script, *args) 在当前窗口/框架 同步 ...
- Selenium with Python 005 - 调用js操作页面元素
WebDriver提供了execute_script()方法来执行JavaScript方法,格式如 driver.execute_script(script,*args) 执行js一般有两种场景,一是 ...
- Java&Selenium自动化测试调用JS实现单击
Java&Selenium自动化测试调用JS实现单击 /* * the method of invoking js to do something * * @author davieyang ...
- Java&Selenium调用JS实现高亮被操作页面元素高亮
Java&Selenium调用JS实现高亮被操作页面元素高亮 /* * the method of invoking js to do something * * @author daviey ...
- Selenium中三种等待的使用方式---规避网络延迟、代码不稳定问题
在UI自动化测试中,必然会遇到环境不稳定,网络慢的情况,这时如果你不做任何处理的话,代码会由于没有找到元素,而报错.这时我们就要用到wait(等待),而在Selenium中,我们可以用到一共三种等待, ...
- iOS:在OC中调用JS脚本
示例一:在webView中调用js脚本进行搜索 1.首先导入JavaScriptCore.framework这个框架 2.创建webView.设置代理.请求手机端百度 #import "Vi ...
- JS调用OC方法并传值,OC调用JS方法并传值////////////////////////zz
iOS开发-基于原生JS与OC方法互相调用并传值(附HTML代码) 最近项目里面有有个商品活动界面,要与web端传值,将用户在网页点击的商品id 传给客户端,也就是js交互,其实再说明白一点 ...
- Magento中调用JS文件的几种方法
一.全局调用方法: 通过该方法每个页面都会引用这个JS文件,除非是类似jQuery这样的系统文件,不然不推荐这种方法. 文件路径:/app/design/frontend/default/Your_T ...
随机推荐
- Ubuntu 16.04 更换阿里云源
Ubuntu 16.04 更换阿里云源sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak #备份sudo gedit /etc/apt/so ...
- 动态向SqlParameter 里添加相应参数
先定义一个List,然后再往List里面添加SqlParameter对象,然后将List转为SqlParameter数组即可 List<SqlParameter> ilistStr = n ...
- nib文件的默认搜索规则
if you do not specify a nib name, and do not override the loadView method in your custom subclass, t ...
- iOS开发之--最简单的导航按钮更换方法/导航颜色的改变
有很多时候,我们需要用到导航,那么更换导航的时候,是在那用那修改,还是自定义一个导航,或者是声明一个代理方法,经过查资料和对导航属性的一些了解,用一种方法最为简单,就是在入口类里面添加一个方法,调用偏 ...
- UE4打包程序没有声音-需要安装UE4PrereqSetup_x64.exe
一个UE4工程打包之后,放到一台新机器,最好安装一下UE4自带的Prerequisites,否则可能会出现没有声音的问题 此安装程序位于WindowsNoEditor\Engine\Extras\Re ...
- ios 的EditBox点击空白处不隐藏的解决方案
原因:参数少了前缀CC 解决方案:修改 cocos/platform/ios/CCEAGLView-ios.mm 中的 handleTouchesAfterKeyboardShow -(void) h ...
- 递归删除资源树 Ztree
前言 最近项目里有这么一个需求:现在有一个用Ztree编写的资源树,当删除资源树的某个节点时,则将此节点下面的所有节点全部删除,这里显然就用到了递归:若此节点被删除后无其它的兄弟节点了,我们还需要将其 ...
- c++多线程例(互斥体,共同访问)
<pre name="code" class="cpp">//这是2个线程模拟卖火车票的小程序 #include <windows.h> ...
- rimraf删除gulp的模块插件
1.安装:npm install -g rimraf(全局安装),如果安装了cnpm,也可使用cnpm install -g rimraf 命令 2.使用:先定位目标文件夹的父级目录,然后命令行输入r ...
- 【BZOJ4146】[AMPPZ2014]Divisors
[BZOJ4146][AMPPZ2014]Divisors Description 给定一个序列a[1],a[2],...,a[n].求满足i!=j且a[i]|a[j]的二元组(i,j)的个数. In ...