0

I am on a side project to write python script to auto-login a website.

I am trying to use python script to launch FireFox browser and auto-login into a website

Below is the source code of the element i'm trying to locate: source code I used below

item_email= WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH, "//input[contains(@id,'username')]")))

or below

item_email= WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH, "//input[contains(@id,'username')]")))

or below

item_email = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH, "//input[@id='j_id0:j_id5:loginComponent:loginForm:username']")))

They all ended up with selenium.common.exceptions.TimeoutException

I was able to locate elements when the input ID didn't have colons (not with namespace, i guess)

AbiSaran
  • 2,538
  • 3
  • 9
  • 15
erhai950
  • 1
  • 1
  • yeah explicit wait will always end up in `selenium.common.exceptions.TimeoutException` exception if the element is not found in HTMLDOM. – cruisepandey Feb 08 '23 at 04:34

2 Answers2

0

erhai950,

Explicit wait will always end up in selenium.common.exceptions.TimeoutException exception if the element is not found in HTMLDOM for any reason.

You can try the below xpath:

//input[contains(@id,'username') and @placeholder='Email']

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste and see //input[contains(@id,'username') and @placeholder='Email'] if your desired element is getting highlighted or not.

Now if we have 1/1 matching entry then try below code:

item_email = driver.find_element(By.XPATH, "//input[contains(@id,'username') and @placeholder='Email']")

or

item_email= WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH, "//input[contains(@id,'username') and @placeholder='Email']")))

Also check if this web element is in iframe or not.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Thanks for answering. Unfortunately I am still unable to get it work. It looks like it's due to the website developed using Angular and will redirect to a different URL the XPATH won't work. Using the similar XPATH worked before the website updated. I guess I have have to use a different method than XPATH – erhai950 Feb 09 '23 at 18:13
0

Given the HTML:

html

the element is a <input> element which would accept text input.


Solution

Ideally to send a character sequence to an <input> element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.form-group input[id$='username'][name$='username'][placeholder='Email']"))).send_keys("erhai950")
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='form-group']//input[contains(@id, 'username') and contains(@name, 'username')][@placeholder='Email']"))).send_keys("erhai950")
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for answering. Unfortunately I am still unable to get it work. It looks like it's due to the website developed using Angular and will redirect to a different URL the XPATH won't work. Using the similar XPATH worked before the website updated. I guess I have have to use a different method than XPATH – erhai950 Feb 09 '23 at 18:12
  • @erhai950 Update the question with the error you observe with this code suggested in this answer. – undetected Selenium Feb 09 '23 at 18:41