尝试使用Selenium Web使用Javascript启动网页 我正在使用Chrome驱动程序。
由于某种原因,它不会启动该网站。我确保包含了称为Javascript所需的所有软件包。
import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.JavascriptExecutor;
public class JavaScriptSelenium {
WebDriver achromeDriver;
String abaseUrl;
private JavascriptExecutor js;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
//abaseUrl = "http://letskodeit.teachable.com/pages/practice";
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\ChromeDirver\\chromedriver.exe");
achromeDriver = new ChromeDriver();
achromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
achromeDriver.manage().window().maximize();;
System.out.println("setup completed");
}
@After
public void tearDown() throws Exception {
}
@Test
public void test() {
System.out.println("in Test");
js.executeScript("window.location ='https://www.google.com'");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WebElement atextbox = (WebElement)js.executeScript("return document.getElementById('name')");
atextbox.sendKeys("Hello world");
}
}
答案 0 :(得分:1)
在@Test方法中,您必须将驱动程序分配给JavaScript执行程序,并添加
JavascriptExecutor js =(JavascriptExecutor)achromeDriver; js.executeScript(“ window.location ='https://www.google.com'”);