所以我一直在尝试制作一个可以与网页交互的程序来输入数据。我理想地想使用Chrome,所以我尝试设置Selenium WebDriver和ChromeDriver。
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Chrome {
public static void main(String[] args) {
//Set chromedriver path
System.setProperty("webdriver.chrome.driver","C:/Users/Username/Desktop/Comp Sci work/chromedriver.exe");
WebDriver driver = new ChromeDriver();
// Open Google
driver.get("http://www.google.com");
// Maximize browser
driver.manage().window().maximize();
}
}
我似乎正确设置了外部JAR,因为我可以毫无问题地导入它们。问题是由于某些原因无法创建Chrome进程。我认为这可能是因为已经有Chrome进程打开但没有。当我杀死进程时,我仍然遇到同样的错误。
然后我尝试设置重置Chrome的路径,因为默认的路径可能与我的不同,但仍然没有运气。
public class Chrome {
public static void main(String[] args) {
//Set chromedriver path
System.setProperty("webdriver.chrome.driver","C:/Users/Username/Desktop/Comp Sci work/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setBinary("C:\\Users\\Username\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
WebDriver driver = new ChromeDriver();
// Open Google
driver.get("http://www.google.com");
// Maximize browser
driver.manage().window().maximize();
}
}
错误消息是:
Starting ChromeDriver 2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e)
on port 43997
Only local connections are allowed.
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown
error: Failed to create a Chrome process.
(Driver info: chromedriver=2.41.578737
(49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134
x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 199 milliseconds
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-
02T20:05:20.749Z'
由于chromedriver似乎开始很好,问题只是创建chrome进程,但我似乎无法找出原因。任何帮助将不胜感激(还有关于我的帖子格式的提示,因为这是我的第一篇文章)。谢谢
我今天遇到了这个问题,并最终解决了它。因为chrome运行为Administrator.so java无法启动它。
Google Chrome Properties-> Compatibility->不以管理员身份运行