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)

Most upvoted comments

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)

from sbvirtualdisplay import Display
from seleniumbase import Driver

display = Display(visible=0, size=(1440, 1880))
display.start()

driver = Driver()
try:
    driver.open("seleniumbase.github.io/demo_page")
    driver.highlight("h2")
    driver.type("#myTextInput", "Automation")
    driver.click("#checkBox1")
    driver.highlight("img", loops=6)
finally:
    driver.quit()

display.stop()

There’s a popular StackOverflow post about it: https://stackoverflow.com/a/73840130/7058266

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=True or xvfb=True. (By default, it was probably using headless2=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.)

Screenshot 2023-11-07 at 12 39 19 PM

The officially-supported way of doing multithreading in UC Mode is by using pytest (via pytest-dist). Since it looks like you’re setting multiple simultaneous proxies, you’ll need to use --multi-proxy / multi_proxy=True with that. (See https://github.com/seleniumbase/SeleniumBase/issues/1832)

Depending on your Ubuntu configuration, you may need to use --xvfb / xvfb=True instead of using headless mode. Headless mode is the default on Ubuntu, but there is a --headed / headed=True option to override.

You may be overcomplicating your selenium-stealth setup. You can integrate it directly into the custom setUp() method of your tests like this: (A BaseCase format lets you use proper multithreading with pytest / pytest-xdist.)

from seleniumbase import BaseCase
from selenium_stealth import stealth

class BaseTestCase(BaseCase):
    def setUp(self):
        super().setUp()
        stealth(self.driver,
            languages=["en-US", "en"],
            vendor="Google Inc.",
            platform="Win32",
            webgl_vendor="Intel Inc.",
            renderer="Intel Iris OpenGL Engine",
            fix_hairline=True,
        )

Then have your test classes inherit BaseTestCase instead of BaseCase. (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, or EC.presence_of_element_located anywhere in your code. Use the built-in methods instead. For the raw driver formats, 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 the pytest formats, see any example test that starts with test_ or ends with _test in the SeleniumBase/examples folder.

Also note that you have unused code in your examples. You declared options with options = webdriver.ChromeOptions(), but never used it. You shouldn’t be using that at all since SeleniumBase options are passed via pytest command-line args, or via method args, eg. driver = Driver(uc=True).

Finally, selenium-stealth is 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.