Home > Enterprise >  Selenium: Can't locate element with wait timeout set
Selenium: Can't locate element with wait timeout set

Time:01-12

Thanks for your time. It's a bit long, really appreciated.

I couldn't locate the element. I tried setting wait, switch frame, executing js

Here is part of the HTML DOM after the page is fully loaded: (the DOM inside iframe is omitted now)

<body >
        <div id="display" >
            <div  style="min-height: 300px">
                <f-icon ></f-icon>
            </div>
        <iframe style="border: 0px; height: 100%; position: absolute; width: 100%;">#document</iframe>
<div >
        <div >
                <div >
                        <p>Enter your credentials</p>
                        <input id="user" type="text" placeholder="Username">
                        <input id="pass" type="password" placeholder="Password">
                </div>
                <div >
                        <button  type="button">Login</button>
                        <button type="button">Close Window</button>
                </div>
        </div>
</div>
</div>
<script src="/4517d062d20772b8f56b8a652474eaed/js/legacy_theme_setup.js"></script></body>

Here is what I want to achieve in python

ele = driver.find_element(By.CSS_SELECTOR, "#user")

Error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: 
Unable to locate element: {"method":"css selector","selector":"#user"}

Then I tried setting wait

ele = WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#user")))
#or
ele = WebDriverWait(driver, 60).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "#user")))

Both got the same output:

[22112:23972:0111/131341.033:ERROR:chrome_browser_main_extra_parts_metrics.cc(227)] START: ReportBluetoothAvailability(). If you don't see the END: message, this is crbug.com/1216328.
[22112:23972:0111/131341.033:ERROR:chrome_browser_main_extra_parts_metrics.cc(230)] END: ReportBluetoothAvailability()
[22112:23972:0111/131341.034:ERROR:chrome_browser_main_extra_parts_metrics.cc(235)] START: GetDefaultBrowser(). If you don't see the END: message, this is crbug.com/1216328.
[22112:1616:0111/131341.059:ERROR:device_event_log_impl.cc(214)] [13:13:41.059] Bluetooth: bluetooth_adapter_winrt.cc:1075 Getting Default Adapter failed.
[22112:23972:0111/131341.131:ERROR:chrome_browser_main_extra_parts_metrics.cc(239)] END: GetDefaultBrowser()

Traceback (most recent call last):
  File "C:\Users\a.py", line 60, in <module>
    ele = WebDriverWait(driver, 60).until(EC.element_to_be_clickable(
  File "C:\Users\...\wait.py", line 89, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException

It says time out, so it didn't find the element within the time limit(60s)

Then I tried locating the element in the browser js console, it succeeded.

document.getElementById('pass')
<input id=​"pass" type=​"password" placeholder=​"Password">​
document.getElementById('pass').value = '123456'
'123456'

enter image description here

I couldn't validate my idea, since the HTML code you placed isn't interactable.

I've modified the HTML setting up a default value in this item and it's not giving the value, however, I'm able to get the position and others values from this web element. I would advise to remove the switch method and then double check.

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

driver = webdriver.Chrome('chromedriver.exe')  # Optional argument, if not specified will search path.
try:
    driver.get('C:\\Users\\user\\IdeaProjects\\chrome-selenium-python\\index.html')
    time.sleep(2)  # Let the user actually see something!

    ele = driver.find_element(By.CSS_SELECTOR, "#user")
    ele.send_keys("Example")
    time.sleep(2)  
    print("Location", ele.location)
    print("Tag", ele.tag_name)
except Exception as a:
    print(a)
finally:
    driver.quit()

CodePudding user response:

wait=WebDriverWait(driver,30)                                 
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"#id"))).send_keys('user')

Try for presence.

Imports:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
  •  Tags:  
  • Related