我试图用不同的帐户访问一些网站,并避免每次我计划为每个网站创建用户配置文件时都登录。我在Ubuntu服务器上托管我的Python应用程序,所以我必须无头运行它,而且我还需要移动仿真。以下是我正在尝试做的一个示例:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_experimental_option("mobileEmulation", {"deviceName": "Pixel 2"})
options.add_argument("user-data-dir=account-data/account_3")
chromedriver = "./webdrivers/chrome"
driver = webdriver.Chrome(executable_path=chromedriver, options=options)
print("START SEARCH")
# Google is an example, but nothing works
driver.get("https://www.google.com")
print("FINISHED")
我看到了START SEARCH
,但从未看到FINISHED
。然而,当我注解user-data-dir
选项时,一切都运行得很完美。奇怪的是,这发生在Ubuntu 20 LTS服务器上,但在我的Mac上没有问题。
在试图获得这个问题的版本时,突然出现了一个不起作用的版本,它给了我这样的信息:
Traceback (most recent call last):
File "seleniumtest.py", line 12, in <module>
driver = webdriver.Chrome(executable_path=chromedriver, options=options)
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist
(Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 5.4.0-1009-kvm x86_64)
在收到这个错误之前,我唯一真正做过的事情就是删除所有的/tmp/.org.chromium.Chromium.*
文件,但这些文件应该只是临时文件,当我没有指定user-data-dir
标志时,应该一直创建新的文件。
4条答案
按热度按时间amrnrhlw1#
原来问题是我没有在
user-data-dir
lag中放入一个绝对路径。o75abkj42#
I don't see any major issue in your code block.
However**
chromedriver=2.41.578700
**is pretty old and ancient.Solution
Ensure that:
@Test
asnon-rootuser.driver.quit()
withintearDown(){}
method to close & destroy the WebDriver and Web Client instances gracefully.ocebsuys3#
与其尝试所有代码,不如先下载chromeDriver并安装它。然后将这条路径放入下面的行中。
请记住,当您执行下面的脚本时,将打开一个新的Chrome窗口,但不会关闭它。
1zmg4dgp4#
对于
Mac
用户(M1、M2)核心-使用
seleniarm/standalone-chromium
而不是selenium/standalone-chrome