当前位置:网站首页>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"));
边栏推荐
猜你喜欢

After Effects 教程,如何在 After Effects 中调整过度曝光的快照?

利用PHP开发具有注册、登陆、文件上传、发布动态功能的网站

Graham‘s Scan法求解凸包问题

研发过程中的文档管理与工具

使用 Postman 工具高效管理和测试 SAP ABAP OData 服务的试读版

【C语言】LeetCode27.移除元素

华为顶级工程师历时9年总结的“趣谈网络协议”PDF文档,太强了

Unity 之 图集属性详解和代码示例 -- 拓展一键自动打包图集工具
![[pytorch] pytorch automatic derivation, Tensor and Autograd](/img/99/c9632a7d3f70a13e1e26b9aa67b8b9.png)
[pytorch] pytorch automatic derivation, Tensor and Autograd

How to switch remote server in gerrit
随机推荐
基于ABP实现DDD
组合学笔记(六)局部有限偏序集的关联代数,Möbius反演公式
Anaconda如何顺利安装CV2
【Yugong Series】July 2022 Go Teaching Course 022-Dictionary of Go Containers
t-sne 数据可视化网络中的部分参数+
复制延迟案例(1)-最终一致性
6. 使用 Postman 工具高效管理和测试 SAP ABAP OData 服务
How to switch remote server in gerrit
上传图片-微信小程序(那些年的坑记录2022.4)
2020微信小程序反编译教程(小程序反编译源码能用吗)
[Source code analysis] BeanFactory and FactoryBean
Mariabackup implements incremental data backup for Mariadb 10.3
Intelligent bin (9) - vibration sensor (raspberries pie pico implementation)
Character pointer assignment [easy to understand]
【pytorch】pytorch 自动求导、 Tensor 与 Autograd
仿生毛毛虫机器人源码
T - sne + data visualization parts of the network parameters
MySQL multi-table union query
联邦学习:联邦场景下的多源知识图谱嵌入
arm按键控制led灯闪烁(嵌入式按键实验报告)