使用Selenium操作JavaScript进行点击
来源:互联网 发布:产品复制软件 编辑:程序博客网 时间:2024/06/09 17:55
package com.gloryroad.Demo;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class JavaScriptExcutor {
/**
* 解决在某些环境下,使用click方法无效的情况
*/
public WebDriver driver;
private String url="http://www.sogou.com";
@BeforeMethod
public void setUp(){
driver=new FirefoxDriver();
driver.get(url);
}
@AfterMethod
public void tearDown(){
driver.close();
}
@Test
public void testHandlerFrame(){
WebElement searchinputbox=driver.findElement(By.id("query"));
WebElement searchButton=driver.findElement(By.id("stb"));
searchinputbox.sendKeys("使用JavaScript语句进行");
JavaScriptClick(searchButton);
}
public void JavaScriptClick(WebElement element){
try {
if (element.isEnabled() && element.isDisplayed()) {
((JavascriptExecutor)driver).executeScript("arguments[0].click();",element );
}else {
System.out.println("页面上的元素无法进行点击操作");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class JavaScriptExcutor {
/**
* 解决在某些环境下,使用click方法无效的情况
*/
public WebDriver driver;
private String url="http://www.sogou.com";
@BeforeMethod
public void setUp(){
driver=new FirefoxDriver();
driver.get(url);
}
@AfterMethod
public void tearDown(){
driver.close();
}
@Test
public void testHandlerFrame(){
WebElement searchinputbox=driver.findElement(By.id("query"));
WebElement searchButton=driver.findElement(By.id("stb"));
searchinputbox.sendKeys("使用JavaScript语句进行");
JavaScriptClick(searchButton);
}
public void JavaScriptClick(WebElement element){
try {
if (element.isEnabled() && element.isDisplayed()) {
((JavascriptExecutor)driver).executeScript("arguments[0].click();",element );
}else {
System.out.println("页面上的元素无法进行点击操作");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
0 0
- 使用Selenium操作JavaScript进行点击
- 使用selenium模块模拟浏览器爬去网页,并进行点击定位内容笔记
- selenium使用Xpath+CSS+JavaScript+jQuery的定位方法(治疗selenium各种定位不到,点击不了的并发症)
- selenium使用Xpath+CSS+JavaScript+jQuery的定位方法(治疗selenium各种定位不到,点击不了的并发症)
- selenium使用Xpath+CSS+JavaScript+jQuery的定位方法(治疗selenium各种定位不到,点击不了的并发症)
- selenium使用Xpath+CSS+JavaScript+jQuery的定位方法(治疗selenium各种定位不到,点击不了的并发症)
- 使用javascript对cookie进行操作
- 使用Selenium操作Cookie
- 使用Selenium进行验收测试
- 使用Selenium进行验收测试
- 使用Selenium进行端到端测试
- selenium+python使用js点击元素
- 点击控件进行计时操作
- selenium使用JavascriptExecutor对按钮上显示的字进行更改操作
- ubuntu使用selenium自动化操作
- Selenium WebDriver中常用到的JavaScript操作
- 使用Javascript和PHP对服务端XML文档进行操作
- 使用JavaScript进行基本图形操作与处理
- 学习方法(公司大神推荐学习算法)
- 抽象类和接口的区别
- 一致性哈希算法(consistent hashing)
- JVM类加载机制
- Druid用java代码配置
- 使用Selenium操作JavaScript进行点击
- 3.2 Machine Programming
- Java中堆内存和栈内存的区别
- C++/C#控制台输入输出
- ios 字符串包含某个字符
- swing 控件邦定验证
- 如何写好一篇SCI论文
- undefined与null的区别(转载)
- 极简攻略—VS2012中配置OpenGL环境