当前位置:网站首页>A common method and the use of selenium
A common method and the use of selenium
2022-07-31 16:39:00 【Xiaopaicai learns java】
selenium的常用方法
ChromeDriver driver = new ChromeDriver();1,获取当前页面的路径
String url1 = driver.getCurrentUrl();2,获取当前页面的标题
String title1 = driver.getTitle();3,Jump to the latest page
//The only handle every page yourself,So can handle to page,跳转到指定页面
String window = driver.getWindowHandle();//获取当前页面的句柄
Set<String> windowHandles = driver.getWindowHandles();//Get all the handles of visited pages
for (String handle : windowHandles) {
if (!window.equals(handle)){
System.out.println("Jump to the latest open page");
driver.switchTo().window(handle);//跳转到新的页面
System.out.println("页面的句柄:" + handle);
}
}4,窗口大小
driver.manage().window().maximize();//窗口最大化
driver.manage().window().minimize();//窗口最小化
driver.manage().window().setSize(new Dimension(800,600));//Set the specified size5,selenium执行脚本
driver.executeScript("window.scroll(0,document.body.scrollTop)");
driver.executeScript("window.scroll(0,document.body.scrollHeight)");6,导航(nacigate接口)
1),前进:driver.navigate().forword()
2),后退driver.navigate().forword()
driver.get("https://tool.lu/");
Thread.sleep(2000);
driver.findElement(By.cssSelector("#nav > div > div > ul > li:nth-child(1) > a")).click();
Thread.sleep(2000);
driver.navigate().back();//后退
Thread.sleep(2000);
driver.navigate().forward();//前进7,弹窗(Alert接口)
1),Ordinary popup window operating train of thought:
driver.get("网页的url");//The binding site
WebElement element = driver.findElement(By.cssSelector("The pop-up Windows buttonclass"));
element.click();//点击按钮
WebElement element1 = driver.findElement(By.cssSelector("Find close pop-up properties"));
element.click();//点击关闭弹窗2),Warning popup window handle ideas:
driver.get("网页的url");
driver.findElement(By.xpath("Click the link will appear warning popup windowxpath")).click();
Alert alert = driver.switchTo().alert();//Get popup window events
//Warning popup windowclass和idCan't be segment code location,So click don't need to find the propertiesxpath或这css样式
//直接接收
alert.accept();//点击确认按钮
alert.dismiss();//点击取消3),Input popup window handle ideas:

driver.get("网页的url");
driver.findElement(By.xpath("Click the link will appear warning popup windowxpath")).click();
Alert alert = driver.switchTo().alert();//Get popup window events
//Warning popup windowclass和idCan't be segment code location,So click don't need to find the propertiesxpath或这css样式
//直接接收
alert.sendKeys("To input text information");//
alert.accept();//点击确认按钮
alert.dismiss();//点击取消8,鼠标键盘操作(Actions接口)
selenuim提供actionsTo simulate mouse and keyboard operation
driver.get("To test the web");
WebElement element = driver.findElement(By.cssSelector("Want to operating elements"));
Actions actions = new Actions(driver);//Common simulated mouse keyboard
actions.clickAndHold(element).perform();//Move the mouse to the location of the need to manipulate and maintain,演示
actions.click(element).perform();//Demo click effect
actions.sendKeys("键盘的操作").perform();//From analog keyboard input9,Simulation check box(Select接口)
driver.get("To test the web");
WebElement element = driver.findElement(By.cssSelector("复选框classID"));
Select select = new Select(element);
select.selectByIndex(1);//Through the index selection list items,索引从0开始
select.selectByValue("name");//通过valueValue to select a list item
select.selectByVisibleText("The list items the text content of");//Through the visible text selection10,文件的上传
seleniumDogs can't pull up the system of other program window,因此可以使用sendKeys(“文件路径”)上传文件
driver.get("To test the web");
WebElement element = driver.findElement(By.cssSelector("上传文件的classID"));
element.sendKeys("文件路径");//seleniumCan't pull system of other program,With the method of input file path,上传文件11,页面刷新
driver.get("To test the web");
driver.navigate().refresh();//刷新页面12,屏幕截图
driver.get("http://www.baidu.com");
driver.findElement(By.cssSelector("#kw")).sendKeys("selenium");
driver.findElement(By.cssSelector("#su")).click();
//屏幕截图
File file = new File("./src/test/screenShut/my.png");
File screenshot = driver.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenshot,file);
} catch (IOException e) {
e.printStackTrace();
}
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));
driver.findElement(By.cssSelector("#\\31 > div > div > h3 > a"));
边栏推荐
- 多主复制下处理写冲突(1)-同步与异步冲突检测及避免冲突
- Automated testing - web automation - first acquaintance with selenium
- Three aspects of Ali: How to solve the problem of MQ message loss, duplication and backlog?
- tensorflow2.0 cnn(layerwise)
- The new BMW 3 Series is on the market, with safety and comfort
- 利用PHP开发具有注册、登陆、文件上传、发布动态功能的网站
- 动态规划之线性dp(上)
- 2.索引及调优篇【mysql高级】
- 【TypeScript】深入学习TypeScript类型操作
- C language - function
猜你喜欢

【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】

gerrit中如何切换远程服务器

基于ABP实现DDD
How Redis handles concurrent access

Premiere Pro 2022 for (pr 2022)v22.5.0

全新宝马3系上市,安全、舒适一个不落

Implementing distributed locks based on Redis (SETNX), case: Solving oversold orders under high concurrency

t-sne 数据可视化网络中的部分参数+
![[TypeScript] In-depth study of TypeScript type operations](/img/d9/ee240ccba72e8d3114ee5c52ed0c8f.png)
[TypeScript] In-depth study of TypeScript type operations

华为顶级工程师历时9年总结的“趣谈网络协议”PDF文档,太强了
随机推荐
Website vulnerability repair service provider's analysis of unauthorized vulnerability
Flutter set the background color of the statusbar status bar and APP method (AppBar) internal consistent color.
Replication Latency Case (3) - Monotonic Read
自动化测试—web自动化—selenium初识
Graham's Scan method for solving convex hull problems
研发过程中的文档管理与工具
Qt实战案例(54)——利用QPixmap设计图片透明度
【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】
npm安装时卡在sill idealTree buildDeps,npm安装速度慢,npm安装卡在一个地方不动
EF Core 2.2中将ORM框架生成的SQL语句输出到控制台
[Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development
Mariabackup实现Mariadb 10.3的增量数据备份
【愚公系列】2022年07月 Go教学课程 020-Go容器之数组
Design and Implementation of Compiler Based on C Language
对话庄表伟:开源第一课
[Source code analysis] BeanFactory and FactoryBean
After the form is submitted, the page does not jump [easy to understand]
[7.28] Code Source - [Fence Painting] [Appropriate Pairs (Data Enhanced Version)]
C程序是如何跑起来的01 —— 普通可执行文件的构成
Premiere Pro 2022 for (pr 2022)v22.5.0