当前位置:网站首页>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"));
边栏推荐
- .NET 20周年专访 - 张善友:.NET 技术是如何赋能并改变世界的
- 【源码解析】BeanFactory和FactoryBean
- GP 6总体架构学习笔记
- How C programs run 01 - the composition of ordinary executable files
- 自动化测试—web自动化—selenium初识
- flutter设置statusbar状态栏的背景颜色和 APP(AppBar)内部颜色一致方法。
- How Redis handles concurrent access
- Intelligent bin (9) - vibration sensor (raspberries pie pico implementation)
- Replication Latency Case (1) - Eventual Consistency
- 多主复制下处理写冲突(4)-多主复制拓扑
猜你喜欢
![[TypeScript]OOP](/img/d7/b3175ab538906ac1b658a9f361ba44.png)
[TypeScript]OOP

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

基于ABP实现DDD

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

.NET 20th Anniversary Interview - Zhang Shanyou: How .NET technology empowers and changes the world

EF Core 2.2中将ORM框架生成的SQL语句输出到控制台

苹果官网样式调整 结账时产品图片“巨大化”

Qt实战案例(54)——利用QPixmap设计图片透明度

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

C程序是如何跑起来的01 —— 普通可执行文件的构成
随机推荐
Dialogue with Zhuang Biaowei: The first lesson of open source
入职一个月反思
After Effects 教程,如何在 After Effects 中调整过度曝光的快照?
【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发
无主复制系统(3)-Quorum一致性的局限性
LevelSequence源码分析
【愚公系列】2022年07月 Go教学课程 022-Go容器之字典
小程序:matlab解微分方程「建议收藏」
【TypeScript】深入学习TypeScript类型操作
仿生毛毛虫机器人源码
Graham's Scan method for solving convex hull problems
Huawei's top engineers lasted nine years "anecdotal stories network protocol" PDF document summary, is too strong
使用 Postman 工具高效管理和测试 SAP ABAP OData 服务的试读版
【源码解析】BeanFactory和FactoryBean
外媒所言非虚,苹果降价或许是真的在清库存
How to switch remote server in gerrit
ansible study notes 02
【愚公系列】2022年07月 Go教学课程 020-Go容器之数组
Replication Latency Case (3) - Monotonic Read
最后写入胜利(丢弃并发写入)