SeleniumWebDriver-JavaScriptExecutor用法
来源:互联网 发布:小说书籍知乎 编辑:程序博客网 时间:2024/06/09 23:42
使用JavaScriptExecutor点击页面上的元素
package Driver_Web;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
/**
* 使用JavaScriptExecutor点击元素
* */
public class JavaScriptExecutorClickElement {
WebDriver driver;
@BeforeMethod
public void beforeMethod() {
System.setProperty(“webdriver.chrome.driver”, “./rcs/chromedriver.exe”);
driver = new ChromeDriver();
driver.get(“https://www.baidu.com“);
}
@AfterMethod
public void afterMethod() throws InterruptedException {
Thread.sleep(5000);
driver.quit();
}
@Test
public void testJavaScriptExecutor() {
driver.findElement(By.id(“kw”)).sendKeys(“胡因梦”);
WebElement element = driver.findElement(By.id(“su”));
//调用封装好的方法来操作这个元素
JavaScriptClick(element);
}
//封装一个方法JavaScriptClick
private void JavaScriptClick(WebElement element) {
try{
if(element.isEnabled()&&element.isDisplayed()){//如果这个元素可以使用并且可以显示的话则执行
System.out.println(“使用JavaScript点击元素”);
((JavascriptExecutor)driver).executeScript(“arguments[0].click();”,element);
}else{
System.out.println(“页面上的元素功能异常”);
}
}catch(StaleElementReferenceException e) //这个报错意思是找得到元素,但是它在当前页面不可见
{
System.out.println(“这个元素在页面上看不见”+e.getStackTrace());
}
catch(NoSuchElementException e)//没有这样的元素
{
System.out.println(“页面上没有这样的元素”+e.getStackTrace());
}
catch(Exception e)
{
System.out.println(“无法完成点击动作”+e.getStackTrace());
}
}
}
- SeleniumWebDriver-JavaScriptExecutor用法
- JavascriptExecutor接口
- seleniumWebDriver的API操作_10_1
- 开源应用架构之SeleniumWebDriver
- seleniumWebDriver的h5元素定位_07
- seleniumWebDriver的API高级应用_11_1
- seleniumWebDriver的API操作显示等待_10_5
- 使用SeleniumWebDriver开始自动化-简单示例
- 使用webdriver中的JavascriptExecutor执行js改变DOM属性
- Java Code Examples for org.openqa.selenium.JavascriptExecutor
- SeleniumWebdriver测试环境配置及第一个脚本实例_02
- seleniumWebDriver的API操作下拉列表Select元素操作_10_2
- seleniumWebDriver自动化测试框架_01TestNG数据驱动测试
- selenium使用JavascriptExecutor对按钮上显示的字进行更改操作
- seleniumWebDriver的API高级应用Log4j打印执行日志11_2
- seleniumWebDriver的API操作杀进程、截屏、执行js脚本_10_4
- seleniumWebDriver自动化测试框架_02TestNG和txt文件进行数据驱动
- seleniumWebDriver自动化测试框架_03TestNG和ApachePOIExcel文件数据驱动测试
- iOS开发-大文件下载与断点下载思路
- xdoj 1193: 杰师傅与锘爷
- 枚举
- 文件下载异常处理
- xdoj 1055: 如此遍历
- SeleniumWebDriver-JavaScriptExecutor用法
- dp基础题
- Cocos2d-x 内存管理机制
- shell变量
- xdoj 1142: 删除字符
- 并查集模板
- Hadoop安装教程
- 最短路径模板
- LeetCoder 13. Roman to Integer