0

If you visit: http://212.238.166.253:8080/

You will notice the following html element:

<input type="password" autocapitalization="off" autocomplete="off" value="" name="login_passwd" tabindex="2" class="form_input" maxlength="16" onkeyup="" onpaste="return false;" onblur="" placeholder="Password">

In python I wrote:

import time

import selenium.common.exceptions as SE
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

PASSWORD_XPATH = "//input[(@type='password') or (@type='Password') or (@type='PASSWORD')]"

if __name__ == '__main__':
    driver = webdriver.Chrome(service=Service(
        '/Users/algo/.wdm/drivers/chromedriver/mac64/100.0.4896.60/chromedriver'))
    driver.get('http://212.238.166.253:8080/')
    time.sleep(5)
    try:
        password_input = WebDriverWait(driver, timeout=5).until(
            EC.element_to_be_clickable((By.XPATH, PASSWORD_XPATH)))
    except SE.TimeoutException:
        print('Nothing was Found!')

But all I get is:

Nothing was Found!

Why is that? I double checked and the page got directly fully uploaded, increasing timeout and sleep time didn't work either. Why the element wasn't found even though it's there?

Roi
  • 49
  • 6

1 Answers1

0

As @Arundeep Chohan mentioned the locator strategy you have used identifies multiple elements within the HTML DOM.

multiple_ifame

where the first matching element is having style="display: none; and ofcoarse isn't your desired element.

<input name="foilautofill" style="display: none;" type="password">

Hence, Nothing was Found! gets printed.


The second matching node is your desired element

<input type="password" autocapitalization="off" autocomplete="off" value="" name="login_passwd" tabindex="2" class="form_input" maxlength="16" onkeyup="" onpaste="return false;" onblur="" placeholder="Password">

You need to construct canonical locator strategies which identifies the desired element uniquely within the DOM Tree.


Solution

To locate the element ideally you need to induce WebDriverWait for the element_to_be_clickable() you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    password_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='password'][name='login_passwd']")))
    
  • Using XPATH:

    password_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@type='password' and @name='login_passwd']")))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • What I don't understand why in case of multiple matches Nothing Found is being printed? shouldn't it wait for first element to be clickable? if Not how can I do this? – Roi Apr 11 '22 at 03:47
  • If your method is looking out for a single webelement, the first matching element is returned irrespective of being visible/invisible, clickable/uninterectable. The solution is to construct a unique locator. – undetected Selenium Apr 11 '22 at 05:52
  • "the first matching element is returned" so why in my case even though the XPATH matches some, nothing found was printed? – Roi Apr 11 '22 at 09:53
  • Selenium can find the element but cannot focus on the element to send text due to the fact `style="display: none;`. Hence that isn't your desired element. – undetected Selenium Apr 11 '22 at 09:55
  • well I have added to the condition if the following doesn't exists in the field attributes: `style="display: none;` still same output. Here try this: `PASSWORD_XPATH = "//input[((@type='password') or (@type='Password') or (@type='PASSWORD') or (@type='text' and contains(@style,'text-security')))" \ " and (not(@disabled)) and (not(contains(@class,'disabled')))" \ " and (not(contains(.,'display: none')))]"` – Roi Apr 11 '22 at 14:22
  • @Roi Seems you are messing up a couple of things. `display: none` isn't the innerText, rather it's the _style_ attribute. What's wrong with _`element_to_be_clickable()`_? – undetected Selenium Apr 11 '22 at 14:25
  • sorry I don't get you, can you kindly fix my xpath without changing its logic? – Roi Apr 11 '22 at 14:33
  • Let's discuss the issue in [Selenium](https://chat.stackoverflow.com/rooms/223360/selenium) room. – undetected Selenium Apr 11 '22 at 14:47
  • I'm inside that room – Roi Apr 11 '22 at 14:56