我刚开始学习 Selenium,但我的 chromedriver 版本似乎与我当前的 chrome 版本不兼容?如能提供任何帮助,我将不胜感激
以下是我收到的错误消息:
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1556601048.968][SEVERE]: Unable to receive message from renderer
org.openqa.selenium.SessionNotCreatedException: session not created
from disconnected: Unable to receive message from renderer
(Session info: chrome=74.0.3729.108)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.17763 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 17.07 seconds
Build info: version: '3.9.1', revision: '63f7b50', time: '2018-02-07T22:42:28.403Z'
我的原始代码:
package com.selenium.webdriver.basic;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class First {
/**
* webdriver is a thing where all my functions are
* which is going to control the browser
* and manages the functions which will let me the browser's property
*/
WebDriver driver;
public void invokeBrowser() {
try {
System.setProperty("webdriver.chrome.driver", "C:\\Selenium_tutorial\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver(); //instantiate your chrome driver
driver.manage().deleteAllCookies();//use driver as object reference
// driver.manage().window().maximize(); //always minimize by default
//bait synchronization. always need this
driver.manage().timeouts().implicitlyWait(3600, TimeUnit.SECONDS); //element detection timeout
driver.manage().timeouts().pageLoadTimeout(3600, TimeUnit.SECONDS);
driver.get("https://www.google.ca/");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
First myObj = new Fisrt();
myObj.invokeBrowser();
}
}
答案1
这可以与旧版本的 Selenium 一起使用,但您应该添加一行并修改另一行以使用 ChromeOptions。我正在执行这个“空中代码”,因此它可能不是 100% 准确:
ChromeOptions option = new ChromeOptions();
driver = new ChromeDriver(options);
您的 IDE 应该提示您包含 ChromeOptions 库,我不知道它是什么,或者我也会为您包含它。
此外,虽然页面和元素超时很重要,但您将两者都设置为一小时 - 对于超时失败来说太长了。我建议将它们设置为更现实的时间,例如 15 到 30 秒。