selenium操作webdriver(一)
在用selenium操作driver,有时候窗口里面会嵌套着一个小窗体,小窗体里面的滚动条下拉时需要先获取小窗体中的某个元素,执行执行点击操作,才可以使其滚动条下拉
首先导入selenium-java的jar包
<dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>3.4.0</version>
</dependency>
代码如下
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;public class Test {public static void main(String[] args) throws Exception{System.out.println("start firefox browser...");System.setProperty("webdriver.chrome.driver","D:/development/chromedriver.exe");ChromeOptions options = new ChromeOptions();options.addArguments("enable-automation");options.addArguments("--disable-infobars");WebDriver driver = new ChromeDriver(options);String url = null;url = "目标网址" ;
// url = "http://www.hao123.com";driver.get(url);System.out.println("title is: "+driver.getTitle());driver.manage().window().maximize();//窗口最大化Thread.sleep(5000);//等待5秒 页面元素加载完成WebElement element = driver.findElement(By.xpath("小窗体中的元素xpath定位"));element.click();//不点击滚动条下拉就不好使,我也是醉了//执行js使窗口滚动条下拉到底部JavascriptExecutor driver_js= (JavascriptExecutor) driver;driver_js.executeScript("window.scrollTo(0,document.body.scrollHeight)");//利用js代码键入搜索关键字
// driver_js.executeScript("document.getElementById(\"kw\").value=\"hello\"");
// driver.findElement(By.id("su")).click();}
}
例子:打开hao123网页,然后下拉到最底端,5秒后关闭
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;public class Test {public static void main(String[] args) throws Exception{System.out.println("start firefox browser...");System.setProperty("webdriver.chrome.driver","D:/development/chromedriver.exe");ChromeOptions options = new ChromeOptions();options.addArguments("enable-automation");options.addArguments("--disable-infobars");WebDriver driver = new ChromeDriver(options);String url = null;url = "http://www.hao123.com";driver.get(url);System.out.println("title is: "+driver.getTitle());//窗口最大化driver.manage().window().maximize();//等待元素页面加载waitForElementToLoad(driver, 5, By.xpath("//*[@id=\"box-weibobottom\"]/div[1]/div[2]/div/h3/a"));//执行js使窗口滚动条下拉到底部JavascriptExecutor driver_js= (JavascriptExecutor) driver;driver_js.executeScript("window.scrollTo(0,document.body.scrollHeight)");Thread.sleep(5000);driver.close();}public static void waitForElementToLoad(WebDriver driver, int timeOut, final By By) {try {(new WebDriverWait(driver, timeOut)).until(new ExpectedCondition<Boolean>() {public Boolean apply(WebDriver driver) {WebElement element = driver.findElement(By);return element.isDisplayed();}});} catch (TimeoutException e) {System.out.println("超时!! " + timeOut + " 秒之后还没找到元素 [" + By + "]");}}
}
selenium操作webdriver(一)相关推荐
- Java selenium操作下拉滚动条的几种方法
数据采集中,经常遇到动态加载的数据,我们经常使用selenium模拟浏览器操作,需要多次下拉刷新页面才能采集到所有的数据,就此总结了几种selenium操作下拉滚动条的几种方法 我这里演示的是Java ...
- selenium操作cookies实现免密登录,自动发微博
一直想用selenium实现个小功能,比如发微博之类的,但是有的网站在登录会有验证码,没想到太好的方法解决,于是想到利用cookies来登录网站 第一步:获取一个可用的cookies,获取的cooki ...
- selenium操作浏览器窗口最大化和刷新
实际测试过程中经常遇到打开一个页面并不是全屏显示,但是却希望它能够全屏显示或者新增一条记录后需要刷新一下看能不能再列表中正常显示. 于是就有了今天的关于对浏览器窗口的最大化和刷新页面.需要说明的一点: ...
- selenium操作浏览器的前进和后退
前进关键字:driver.forward() 后退关键字:driver.back() 测试对象:1.https://www.baidu.com/ 2.https://www.sogou.com/ 实例 ...
- java操作浏览器_java selenium 操作浏览器实例
导读热词 本篇文章介绍selenium 操作浏览器 阅读目录 浏览器最大化 前进,后退, 刷新 截图操作 模拟鼠标操作 杀掉Windows浏览器进程 浏览器最大化 前进,后退, 刷新 public s ...
- web driver selenium 操作滚动条
方法一:调过JS脚本控制 #coding=utf-8 from selenium import webdriver import time #访问百度 driver=webdriver.Firefox ...
- python爬虫selenium操作下拉框详解
使用python爬虫的selenium操作网页的下拉框. 以该网站为例:https://www.17sucai.com/pins/demo-show?id=5926 该网页下存在多个可供测试的下拉框. ...
- Selenium学习 - WebDriver接口
Selenium学习 - WebDriver接口 一.WebDriver的初始化 def __init__(self, command_executor='htt ...
- selenium操作各种浏览器
selenium操作各种浏览器 下载浏览器对应版本的chromedriver.exe放到 %systemroot%目录或其它可以被系统环境找到的目录下 #coding:utf-8 from selen ...
最新文章
- 【HDU - 3870】Catch the Theves(平面图转对偶图最短路,网络流最小割)
- 构造函数的原型和原型链
- jks与keystore的区别
- bs架构多用户访问_Spark架构
- python写入指定路径的文件_python 从shell读取指定文件以及写入指定文件
- 原生javascript开发仿微信打飞机小游戏
- 剧情系统实战,我们要做一个什么样的剧情系统
- 【Research】Accounting-会计
- 零极点和系统稳定性关系
- ie9 ajax 二进制流,2.ajax兼容IE9 非常实用!!!
- staged changes
- APC不间断电源说明书
- 机器学习实战案例—验证码(CAPTCHA)识别基于Logistic
- NMock学习系列(三)--- NMock在DDD领域驱动的单元测试中的应用
- vivo s12参数
- Java面试3-5年规划,去了阿里面试软件测试工程师,面试官问我3-5年的职业规划!...
- zookeeper 删除节点时报错java.io.IOException: Packet len4272892 is out of range!
- 计算机的隐藏游戏怎么找回,文件夹隐藏了怎么恢复,电脑屏幕上文件夹隐藏了怎么恢复...
- 记录-蓝鲸相关知识点
- 【《C Primer Plus》读书笔记】第2章:C语言概述