Categories
Python Answers

How to fix WebDriverException:Chrome failed to start: crashed error with Python Selenium?

Sometimes, we want to fix WebDriverException:Chrome failed to start: crashed error with Python Selenium

In this article, we’ll look at how to fix WebDriverException:Chrome failed to start: crashed error with Python Selenium.

How to fix WebDriverException:Chrome failed to start: crashed error with Python Selenium?

To fix WebDriverException:Chrome failed to start: crashed error with Python Selenium, we set the location of the Chrome binary.

For instance, we write

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.binary_location = "C:\\path\\to\\chrome.exe"   
options.add_argument("--start-maximized")
options.add_argument("--no-sandbox") 
options.add_argument("--disable-dev-shm-usage")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\path\to\chromedriver.exe')
driver.get('http://example.com/')

to set options.binary to the path of the Chrome binary.

We start Chrome maximized with

options.add_argument("--start-maximized")

And then we disable Chrome sandbox with

options.add_argument("--no-sandbox") 

We stop Selenium from being detected with

options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)

Then we create specify the Chrome path again when we create the driver

driver = webdriver.Chrome(options=options, executable_path=r'C:\path\to\chromedriver.exe')
Categories
Python Answers

How to modify navigator.webdriver flag to prevent selenium detection with Python Selenium?

Sometimes, we want to modify navigator.webdriver flag to prevent selenium detection with Python Selenium.

In this article, we’ll look at how to modify navigator.webdriver flag to prevent selenium detection with Python Selenium.

How to modify navigator.webdriver flag to prevent selenium detection with Python Selenium?

To modify navigator.webdriver flag to prevent selenium detection with Python Selenium, we can call add_experimental_options` to set a few options.

For instance, we write

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\path\to\chromedriver.exe')
driver.get("https://www.example.com/")

to call options.add_experimental_option to disable the automation options.

Conclusion

To modify navigator.webdriver flag to prevent selenium detection with Python Selenium, we can call add_experimental_options` to set a few options.

Categories
Python Answers

How to wait until page is loaded with Selenium WebDriver for Python?

Sometimes, we want to wait until page is loaded with Selenium WebDriver for Python In this article, we’ll look at how to wait until page is loaded with Selenium WebDriver for Python

How to wait until page is loaded with Selenium WebDriver for Python?

To wait until page is loaded with Selenium WebDriver for Python, we can use the until method.

For instance, we write

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.get("url")
delay = 3
try:
    myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
    print("Page is ready!")
except TimeoutException:
    print("Loading took too much time!")

to create a WebDriverWait instance with the browser and a 3 seconds delay.

Then we call until with the element to wait for, which we get with

EC.presence_of_element_located((By.ID, 'IdOfMyElement'))

Conclusion

To wait until page is loaded with Selenium WebDriver for Python, we can use the until method.

Categories
Python Answers

How to fix Geckodriver executable needs to be in PATH with Selenium Python?

Sometimes, we want to fix Geckodriver executable needs to be in PATH with Selenium Python.

In this article, we’ll look at how to fix Geckodriver executable needs to be in PATH with Selenium Python.

How to fix Geckodriver executable needs to be in PATH with Selenium Python?

To fix Geckodriver executable needs to be in PATH with Selenium Python, we install Geckodriver and then we can use it in our code.

We install Geckodriver by running

pip install webdriver-manager

And now we can use Geckodriver in our Selenium Python code.

We then use it by writing

from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

Conclusion

To fix Geckodriver executable needs to be in PATH with Selenium Python, we install Geckodriver and then we can use it in our code.

Categories
Python Answers

How to wait until element is present, visible and interactable with Python Selenium?

Sometimes, we want to wait until element is present, visible and interactable with Python Selenium.

In this article, we’ll look at how to wait until element is present, visible and interactable with Python Selenium.

How to wait until element is present, visible and interactable with Python Selenium?

To wait until element is present, visible and interactable with Python Selenium, we can use the wait.until method.

For instance, we write

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#...

browser.find_element_by_css_selector(".reply-button").click()

wait = WebDriverWait(browser, 10)
email = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.anonemail'))).get_attribute("value")
print(email)

to create the wait object with

wait = WebDriverWait(browser, 10)

Then we call wait.until with EC.visibility_of_element_located((By.CSS_SELECTOR, '.anonemail')) to wait for the element with the anonemail class to be visible.

We set the timeout to 10 seconds.

Conclusion

To wait until element is present, visible and interactable with Python Selenium, we can use the wait.until method.