java工作复习——执行JS脚本——点击按钮

package jkcs;

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;

public class jdcs 
{
    
    public static void main(String[] args) throws InterruptedException 
    {
         System.setProperty("webdriver.chrome.bin","‪C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");    //设置安装路径,防止系统找不到
        

         
         WebDriver driver = new FirefoxDriver();
         driver.get("http://www.baidu.com");
         driver.manage().window().maximize();
         
         Thread.sleep(5000);
         
         

         driver.findElement(By.id("kw")).sendKeys("中国");
         
         
         Thread.sleep(5000);
         
         
         
         WebElement element = driver.findElement(By.id("su"));
         
         JavascriptExecutor js = ( JavascriptExecutor ) driver;   //声明一个js执行器
         
         js.executeScript("arguments[0].click();",element); //调用执行器的executeScript方法执行js脚本
         
         
         
         
         
         

         
         
         Thread.sleep(5000);
         
         driver.quit();

    }
}

 

你可能感兴趣的:(java工作复习——执行JS脚本——点击按钮)