import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BrowserTest {
public static void main(String[] args) throws Exception {
String url = "https://kuaibao.qq.com/s/20190519A0I7EZ00";
// ProcessBuilder builder = new ProcessBuilder("C:\\Users\\fuxingyun\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe","https://kuaibao.qq.com/s/20190519A0I7EZ00");
// builder.start();
// System.setProperty("webdriver.chrome.driver","C:\\Users\\fuxingyun\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver","C:/Users/fuxingyun/AppData/Local/Google/Chrome/Application/chromedriver.exe");
WebDriver driver = new ChromeDriver();
// 打开指定的网站
driver.get(url);
// 页面滚动
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(1,1000)");
Thread.sleep(5000);
// 退出浏览器
driver.quit();
// 关闭指定浏览器
// Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");
}
}
代码报以下错,莫名其妙~~~
Exception in thread "main" java.lang.NoSuchFieldError: LINUX
at org.openqa.selenium.net.PortProber.<clinit>(PortProber.java:43)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:351)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:94)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
at com.browser.BrowserTest.main(BrowserTest.java:17)
代码看起来没什么问题,公司大佬说这是底层反射创建对象时没有找到LINUX属性,才报的这个错,可能是导包错误的原因,但是一番百度发现包名看起来和别人的长得一样,然后就陷入了大坑~~~之前用maven下载的依赖,找到一样的包名类名,结果jar包就下载错了!!!原来的jar包是自己在网上随便找的:
<!-- https://mvnrepository.com/artifact/org.openqa.selenium.server/selenium-server -->
<dependency>
<groupId>org.openqa.selenium.server</groupId>
<artifactId>selenium-server</artifactId>
<version>1.0-20081010.060147</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openqa.selenium.webdriver/webdriver-htmlunit -->
<dependency>
<groupId>org.openqa.selenium.webdriver</groupId>
<artifactId>webdriver-htmlunit</artifactId>
<version>0.6.1039</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.141.59</version>
</dependency>
后来找到正确的依赖应该是这样:
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.12.0</version>
</dependency>