-1

I believe this question is unique as this is specifically for Python and not related with the Java issue mentioned in the other thread.

I'm going through Selenium's documentation regarding explicit waits but I can't create code to illustrate each explicit waits use cases.

The example below works (ie returns True)

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Firefox()
driver.get('https://www.google.com')

#match title tag
def title_is(driver, title, timeout=3):
    try:
        w = WebDriverWait(driver, timeout)
        w.until(EC.title_is(title))
        return True
    except:
        return False

print title_is(driver, 'Google',timeout=3)

But the example below doesn't work (I use a different explicit wait condition)

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Firefox()
driver.get('https://www.google.com')

#try to grab <div id="als">
def presence_of_element(driver, timeout=3):
    try:
        w = WebDriverWait(driver, timeout)
        w.until(EC.presence_of_element_located(By.ID('als')))
        return True
    except:
        return False

I've experimented with multiple forms of syntax but I can't make any explicit wait condition to work excepted the title_is

I would really appreciate your feedback as I'm obviously missing something here.

Thanks

doingmybest
  • 314
  • 4
  • 16
  • Possible duplicate of [Replace implicit wait with explicit wait (selenium webdriver & java)](https://stackoverflow.com/questions/45712431/replace-implicit-wait-with-explicit-wait-selenium-webdriver-java) – undetected Selenium Jan 09 '18 at 10:57
  • @DebanjanB OP is not replacing implicit waits... that link doesn't apply. – JeffC Jan 09 '18 at 20:46
  • Your syntax doesn't match the examples in the link you provided. – JeffC Jan 09 '18 at 20:47

2 Answers2

2

The syntax of presence_of_element_located is incorrect. It takes a tuple of a locator's type By.ID and value als

w.until(EC.presence_of_element_located((By.ID, 'als')))

See: http://selenium-python.readthedocs.io/waits.html#explicit-waits

James Whitehead
  • 101
  • 1
  • 11
  • I've tried to correct my syntax using the tuple w.until(EC.presence_of_element_located((By.ID,'als'))) however I still get a False return – doingmybest Jan 09 '18 at 10:02
  • In general, when catching a non-specific exception, it's always a good idea to return the stack trace to help with debugging rather than returning `False`. `False` could mean anything. In this case it was a `NameError` because you didn't include `from selenium.webdriver.common.by import By` in your imports. If you try and run this again with the import, you'll get a `TimeoutException` because 'als' doesn't exist on a page. If you try and locate the div ID 'cst', it'll return True! – James Whitehead Jan 09 '18 at 10:25
  • Fantastic, thank you very much James, I'm posting the solution now based on your feedback – doingmybest Jan 09 '18 at 11:02
0
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
import traceback

driver = webdriver.Firefox()
driver.get('https://www.google.com')

def error_catching():
    traceback.print_stack()
        print '--------------'
        traceback.print_exc()
        return False


#try to grab <div id="gbw">
def presence_of_element(driver, timeout=5):
    try:
        w = WebDriverWait(driver, timeout)
        w.until(EC.presence_of_element_located((By.ID,'gbw')))
        return True
    except:
        error_catching()
doingmybest
  • 314
  • 4
  • 16
  • After someone else pointed out the error in your code you answer your own question and accept this answer? – marianoju Dec 19 '21 at 18:48