Reputation: 113
I am trying to access some websites with different accounts, and to avoid logging in every time I am planning to make an user profile for each one. I am hosting my Python app on an Ubuntu server, so I have to run it headless, and I also need the mobile emulation. Here's a sample of what I'm trying to do:
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")
I see START SEARCH
but never FINISHED
. When I comment the user-data-dir
options, however, everything works perfectly. The weird thing is that this happens on an Ubuntu 20 LTS server, but in my Mac it has no issues.
While trying to get the versions for this question, out of the blue the version that doesn't work started giving me this:
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)
The only thing I've really done before getting this error is deleting all the /tmp/.org.chromium.Chromium.*
files but those should only be temporary files and new ones should be created all the time when I don't specify the user-data-dir
flag.
Upvotes: 2
Views: 12364
Reputation: 325
For Mac
users (M1, M2) cores -
use seleniarm/standalone-chromium
instead of selenium/standalone-chrome
Upvotes: 0
Reputation: 113
Turns out the problem was that I did not put an absolute path in the user-data-dir
flag.
from selenium import webdriver
import os
options = webdriver.ChromeOptions()
path = os.path.abspath("account-data/account_3")
options.add_argument("user-data-dir=" + path)
# other options...
chromedriver = "./webdrivers/chrome"
driver = webdriver.Chrome(executable_path=chromedriver, options=options)
# do stuff...
driver.close()
Upvotes: 2
Reputation: 91
Instead of all your code try this, first download chromedriver, and install it. Then put that path into the line below.
Remember, when you execute the script below, a new chrome window will be opened but do not close it.
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'C:\\Users\\sonal\\.wdm\\drivers\\chromedriver\\80.0.3987.106\\win32\\chromedriver.exe')
driver.get("https://www.google.com")
Upvotes: 0
Reputation: 193078
I don't see any major issue in your code block.
However chromedriver=2.41.578700
is pretty old and ancient.
Ensure that:
@Test
as non-root user.driver.quit()
within tearDown(){}
method to close & destroy the WebDriver and Web Client instances gracefully.Upvotes: 0