我试图用一个URL启动chrome浏览器,浏览器启动后,它什么也不做。
1分钟后我看到如下错误:
Unable to open browser with url: 'https://www.google.com' (Root cause: org.openqa.selenium.WebDriverException: unknown error: DevToolsActivePort file doesn't exist
(Driver info: chromedriver=2.39.562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9),platform=Windows NT 10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information)
我的配置:
Chrome浏览器:66
ChromeBrowser: 2.39.56
又及,在Firefox中一切都很好
我遇到了同样的问题,我使用UBUNTU, PYTHON和OPERA浏览器。在我的例子中,这个问题是因为我有一个过时的operadriver版本。
解决方案:
1. 确保您安装了最新的opera浏览器版本(不要使用opera beta或opera developer),为此请到opera官方网站并从那里下载最新的opera_stable版本。
安装最新的opera驱动程序(如果你已经安装了一个opera驱动程序,你必须先使用sudo rm删除它…)
wget https://github.com/operasoftware/operachromiumdriver/releases/download/v.80.0.3987.100/operadriver_linux64.zip
unzip operadriver_linux64.zip
sudo mv operadriver /usr/bin/operadriver
sudo chown root:root /usr/bin/operadriver
sudo chmod +x /usr/bin/operadriver
在我的情况下,最新的是80.0.3987,你可以看到
此外,我还安装了chromedriver(但因为我在测试之前做了,我不知道这是需要的),为了安装chromedriver,遵循上一步的步骤
好好享受吧,谢谢我!
示例硒代码
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Opera()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.quit()
我也经历过这个问题,提出的解决方案似乎都不起作用。然后我发现问题是我在WSL版本1上运行,似乎chromedriver与windows浏览器一起工作,而不是与aptitude安装的浏览器。
为了使它与WSL版本1和版本2兼容,我发现(并在debian WSLv2和ubuntu WSLv1中进行了测试,它可以工作),该平台的版本显示的单词Microsoft的第一个大写字母。
所以解决方案是这样的:
import platform
from selenium.webdriver.chrome.service import Service as ChromeService
from seleniumwire import webdriver
chromedriver = 'chromedriver.exe'
# According to:
# https://stackoverflow.com/a/71879688/7019069
# When using WSL v1 the chromedriver.exe of local chrome of windows is used
# In WSL v2 (updated version) it does work using the installation shown in the README.md)
# And according to
# https://github.com/microsoft/WSL/issues/4555
# It is possible to differentiate the version of WSL by the first uppercase of the platform
# version of Microsoft. Therefore only the linux chromedriver is used if that word is matched
# in the platform of the driver.
if platform.system() == 'Linux' and not re.search(re.escape('Microsoft'), platform.platform()):
chromedriver = 'chromedriver'
driver_path = os.path.join(drivers_path, chromedriver)
service = ChromeService(driver_path)
driver = webdriver.Chrome(
service=service,
options=__get_chrome_options(headless)
)