SeleniumBase: Message: session not created: Chrome failed to start: exited normally.(session not created: DevToolsActivePort file doesn't exist)(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
I’m sorry for may be being stupid, but…
Bug only on ubuntu 22.04. On Windows 11 no issues!
I have been struggling with this problem for several days:
THIS CODE WORKS ON UBUNTU 22.04:
from selenium import webdriver
from selenium_stealth import stealth
import seleniumbase as sb
def initialize_driver():
try:
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("--headless")
driver = sb.Driver(browser='chrome', headless=True, uc=True)
wait = webdriver.Chrome.implicitly_wait(driver,500.00)
stealth(driver,
languages=["en-EN", "en"],
vendor="Google Inc.",
platform="Win64",
webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
fix_hairline=True,
wait=wait
)
time.sleep(3)
return driver
except:
return None
THIS CODE ALWAYS FAILS ON UBUNTU 22.04:
def initialize_driver(account_number, cookies_file, url, proxy, attempt=1):
if attempt > 5:
return None
try:
driver = sb.Driver(browser='chrome', headless=True, uc=True, proxy=proxy)
wait = webdriver.Chrome.implicitly_wait(driver,500.00)
stealth(driver,
languages=["en-EN", "en"],
vendor="Google Inc.",
platform="Win64",
webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
fix_hairline=True,
wait=wait
)
driver.get(url)
time.sleep(3)
with open(cookies_file, 'r') as f:
cookies = json.load(f)
current_domain = driver.current_url
for cookie in cookies:
if 'sameSite' in cookie and cookie['sameSite'] not in ['Strict', 'Lax', 'None']:
del cookie['sameSite']
if 'domain' in cookie:
if current_domain.find(cookie['domain']) != -1: # Check if the cookie domain is in the current domain
driver.add_cookie(cookie)
driver.refresh()
time.sleep(3)
try:
wait = WebDriverWait(driver, 2)
wait.until(EC.presence_of_element_located((By.XPATH, "//*[contains(normalize-space(), 'Enter or registrate')]")))
driver.quit()
return initialize_driver(account_number, cookies_file, url, proxy, attempt + 1)
except Exception as e:
print(e)
return driver
except Exception as e:
print(e)
driver.quit()
return None
I was debugging alot. On Windows 11 no issues! Only this code can go through Cloudflare on a target site
Thank you!
About this issue
- Original URL
- State: closed
- Created 8 months ago
- Comments: 20 (8 by maintainers)
Thank you. This is a stable solution
After a quick refresher, I recalled that the
Driver()format doesn’t have the Xvfb launcher for Linux like the other SeleniumBase formats. You’ll need to add that in separately: (https://github.com/mdmintz/sbVirtualDisplay)Yes i used it once but in original selenium(some elements were not displayed on screenshots via old headless mode). Gosh…
@mdmintz Thank you so much! I hope you won’t see me with my stupid questions soon=). I will rewrite all my code from selenium-wire to your framework, I like your Tracebacks they are so detailed and helpful
OMG. It worked! driver = sb.Driver(browser=‘chrome’, headless2=True, uc=True, proxy=proxy)
Chrome headless mode doesn’t support extensions. If you have to run on a headless machine, use
headed=Trueorxvfb=True. (By default, it was probably usingheadless2=True, which is Chrome’s newer headless mode.)Greetings. Let’s go through your code and give you some solutions to work with…
You’ve got multiple recursive calls happening with
initialize_driver(), which is certainly going to cause various issues. (See the code below, where I highlighted it in two places.)The officially-supported way of doing multithreading in UC Mode is by using
pytest(viapytest-dist). Since it looks like you’re setting multiple simultaneous proxies, you’ll need to use--multi-proxy/multi_proxy=Truewith that. (See https://github.com/seleniumbase/SeleniumBase/issues/1832)Depending on your Ubuntu configuration, you may need to use
--xvfb/xvfb=Trueinstead of using headless mode. Headless mode is the default on Ubuntu, but there is a--headed/headed=Trueoption to override.You may be overcomplicating your
selenium-stealthsetup. You can integrate it directly into the customsetUp()method of your tests like this: (ABaseCaseformat lets you use proper multithreading withpytest/pytest-xdist.)Then have your test classes inherit
BaseTestCaseinstead ofBaseCase. (See SeleniumBase/help_docs/syntax_formats.md#sb_sf_02)SeleniumBase methods have automatic-waiting. You should never be using the external
implicitly_wait,WebDriverWait, orEC.presence_of_element_locatedanywhere in your code. Use the built-in methods instead. For the rawdriverformats, see examples such as SeleniumBase/examples/raw_login_driver.py, SeleniumBase/examples/raw_driver_manager.py, and SeleniumBase/examples/offline_examples/test_extended_driver.py. For thepytestformats, see any example test that starts withtest_or ends with_testin the SeleniumBase/examples folder.Also note that you have unused code in your examples. You declared
optionswithoptions = webdriver.ChromeOptions(), but never used it. You shouldn’t be using that at all since SeleniumBase options are passed viapytestcommand-line args, or via method args, eg.driver = Driver(uc=True).Finally,
selenium-stealthis an external repo with its own issues. SeleniumBase won’t magically fix those issues if you encounter them while using both frameworks together. To determine between the two, try examples with just SeleniumBase (but not selenium-stealth), and see if those issues still happen.