使用JavaScriptExecutor对象来实现页面元素的单击动作。这种方法主要用于解决在某些情况下,页面元素的.click()方法无法生效。
被测试页面:http://www.baidu.com
实例代码如下:
package cn.om.webdriverapi;
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.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
public class TestClickButtonWithJS {
// 使用JavaScriptExecutor对象来实现页面单击动作。主要用于某些情况下,页面元素的click()方法无法生效
WebDriver driver;
String baseURL;
JavascriptExecutor jsexecutor;
@BeforeMethod
public void beforeMethod() {
baseURL = "http://www.baidu.com";
System.setProperty("webdriver.firefox.bin", "E:/