python selenium checking for element in chrome doesn't work as expected

Issue

This works just fine in Firefox. When I use Chrome, once the page fully loads it doesn’t print "element loaded", and it doesn’t go to timeout either. It just waits forever.
I’ve tried using visibility_of_element_located instead of presence_of_element_located but it makes no difference. I’ve tried all_elements too. Any advice?

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
from selenium.webdriver.common.keys import Keys

browser = webdriver.Chrome()
browser.get("http://url.com")
timeout = 10
email = ("[email protected]")

try:
    email_form_wait = WebDriverWait(browser, timeout).until(EC.presence_of_element_located((By.XPATH, '//*[@id="username"]')))
    print ("element loaded")
    email_form = browser.find_element(By.XPATH, '//*[@id="username"]')
    email_form.send_keys(email, Keys.ENTER)
    
except TimeoutException:
    print ("Loading took too much time!")

Update
For debugging reasons, I’ve been trying this

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

browser = webdriver.Chrome()
browser.get("http://url.com")

time.sleep(5)
print("end wait")

This works as expected on Chrome, but when I add

email_form = browser.find_element(By.XPATH, '//*[@id="username"]')
print("element found")

at the end of the code, it doesn’t even print "end wait", it’s stuck waiting forever and then it prints "end wait" and returns an error only after I force close the browser.

Solution

Site Isolation is a security feature in Chrome that offers additional protection against some types of security bugs. It uses Chrome’s sandbox to make it harder for untrustworthy websites to access or steal information from your accounts on other websites.

Try opening chrome driver with the following options supplied.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

options = webdriver.ChromeOptions()
options.add_argument("--disable-site-isolation-trials")

chrome_path = r"C:\Users\hpoddar\Desktop\Tools\chromedriver_win32\chromedriver.exe"

s = Service(chrome_path)
driver = webdriver.Chrome(service=s, chrome_options=options)

driver.get(url)
username = driver.find_element(By.XPATH, '//input[@id="username"]')
username.send_keys('[email protected]')
submit = driver.find_element(By.CSS_SELECTOR, '.ca56ae105.c03eb9739.c026ac3bb.ca0d6234f._button-login-id')
submit.click()
password = driver.find_element(By.XPATH, '//input[@id="password"]')
password.send_keys('password')
continueButton = driver.find_element(By.CSS_SELECTOR, '.ca56ae105.c03eb9739.c026ac3bb.ca0d6234f._button-login-password')
continueButton.click()

Answered By – Himanshu Poddar

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published