JS的处理
Selenium能够执行js,这使得Selenium拥有更为强大的能力。既然能执行js,那么js能做的事,Selenium应 该大部分也能做
直接使用js操作页面,能解决很多click()不生效的问题
页面滚动到底部,顶部
处理富文本,时间控件的输入
Selenium调用js的过程:
js代码:
window.alert("selenium 弹窗)
a=document.getElementById('KW').value
document.title
selenium提供了js的一个api
JavascriptExecutor jsDriver=(avascriptExecutor)driver;
driver.execute_ script("window.alert('Selenium弹框测试)")
driver.execute. script("a = document.getElementById(kw ).value;window.alert(a)")
返回
jsDriverexecuteScript( return document.getElementById(kw).value")
execute_ script: 执行js
return:可以返回js的返回结果
execute_ script: arguments传参
js处理 时间控件的调用
◆大部分时间控件都是readonly属性,需要手动去选择对应的时间,手工测试中很容易做到,自动化中对控件的操作可以使用js来操作。
◆处理时间控件思路:
1.要取消日期的readonly属性
2.给value赋值
3.写js代码来实现如.上的1,2点,再webdriver对js进 行处理
public class JsTest {
public static WebDriver webDriver;
public static Actions actions;
@BeforeAll
public static void initData() {
webDriver = new ChromeDriver();
actions= new Actions(webDriver);
//设置3秒等待
webDriver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
}
@Test
public void moveTest() throws InterruptedException {
//跳转测试网页
webDriver.get("https://www.12306.cn/index/");
// 通过id获取位置
JavascriptExecutor jswebDriver =(JavascriptExecutor)webDriver;
jswebDriver.executeScript("document.getElementById('train_date').value('2020-08-25')");
System.out.println();
}
@AfterAll
public static void teardowen(){
webDriver.quit();
}
}