当前位置:网站首页>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 size
5,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 input
9,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 selection
10,文件的上传
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"));
边栏推荐
- jeecg主从数据库读写分离配置「建议收藏」
- t-sne 数据可视化网络中的部分参数+
- tensorflow2.0 cnn(layerwise)
- adb shell 报错error: device unauthorized
- Flutter set the background color of the statusbar status bar and APP method (AppBar) internal consistent color.
- LevelSequence源码分析
- SHELL内外置命令
- 最新神作!阿里巴巴刚出炉的面试参考指南(泰山版),我直接狂刷29天
- How to switch remote server in gerrit
- 多主复制下处理写冲突(3)-收敛至一致的状态及自定义冲突解决逻辑
猜你喜欢
随机推荐
外媒所言非虚,苹果降价或许是真的在清库存
Website vulnerability repair service provider's analysis of unauthorized vulnerability
ML.NET相关资源整理
基于ABP实现DDD
牛客 HJ16 购物单
牛客 HJ17 坐标移动
牛客 HJ3 明明的随机数
Single-cell sequencing workflow (single-cell RNA sequencing)
Applicable Scenarios of Multi-Master Replication (1) - Multi-IDC
adb shell 报错error: device unauthorized
多数据中心操作和检测并发写入
二分查找的细节坑
第05章 存储引擎【1.MySQL架构篇】【MySQL高级】
arm按键控制led灯闪烁(嵌入式按键实验报告)
第二届中国PWA开发者日
动态规划(一)
复制延迟案例(3)-单调读
C语言-函数
关于柱状图的经典画法总结
How Redis handles concurrent access