2

I've tried to write a script in python in combination with selenium to wait for a certain element to be available. The content I wish my script waits for is captcha protected. I do not want to set a fixed time. So, I need it to wait until I can solve myself.

I've tried like:

import time
from selenium import webdriver

URL = "https://www.someurl.com/"

driver = webdriver.Chrome()
driver.get(URL)
while not driver.find_element_by_css_selector(".listing-content"):
    time.sleep(1)

print(driver.current_url)
driver.quit()

But, the script throws an error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:

How can I make my script wait until the element is available no matter how long it takes?

7
  • Have you tried running the find_elment_by_css_selector function inside a try/except block inside your while-loop? Commented Feb 9, 2019 at 12:47
  • can you loop while captcha present? Commented Feb 9, 2019 at 12:51
  • Yes, I tried that way @QHarr. The script throws an error as soon as I solve that captcha because that while loop no longer exists and this line driver.find_element_by_css_selector(".listing-content") throws the same error (cosider that the selector contains captcha element). Commented Feb 9, 2019 at 13:10
  • Same within a try block? Commented Feb 9, 2019 at 13:12
  • I can't organize that try/except block in the right way. How can try/except block run indefinitely? Commented Feb 9, 2019 at 13:16

3 Answers 3

2

If you don't want to hardcode wait time you can use ExplicitWait along with float("inf") which in Python stands for INFINITY:

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

wait(driver, float("inf")).until(EC.presence_of_element_located((By.CLASS_NAME, "listing-content")))
Sign up to request clarification or add additional context in comments.

Comments

2

As you've asked how to organize the try/except block, here is an idea. I would suggest to stick with the inf-wait method however.

while True:
    try:
        driver.find_element_by_css_selector(".listing-content")
        break
    except:
        time.sleep(0.1)

I would include the time.sleep() statement to minimize your number of function calls.

1 Comment

if ret: is redundant here: break will be executed ONLY if ret = driver.find_element_by_css_selector(".listing-content") doesn't raise an exception
1

You should use WebDriverWait:

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

element = WebDriverWait(driver, 10000).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".listing-content")))

It will not wait indefinitely, but you can set the timeout high. Otherwise you could try to use the WebDriverWait statement in a loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.