Selenium webdriver python find element by xpath - Could not find element
Question: I m trying to write a script with selenium webdriver python. When I try to do
a

    
    
    find_element_by_xpath("//*[@id='posted_1']/div[3]") 
    

it says

> NoElementFoundException.

Can someone please help me here?

Regards Bala


Answer: that exception, unsurprisingly, means that that element wasn't available on
the DOM. There are a couple of options here:

    
    
    driver.implicitly_wait(10)
    

will tell the driver to wait 10 seconds (or any amount of time) after an
element is not found/not clickable etc., and tries again after. Sometimes
elements don't load right away, so an implicit wait fixes those types of
problems.

The other option here is to do an explicit wait. This will wait until the
element appears, and until the existence of that element is confirmed, the
script will not move on to the next line:

    
    
    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(ff, 10).until(EC.presence_of_element_located((By.XPATH, "//*[@id='posted_1']/div[3]")))
    

In my experience, an implicit wait is usually fine, but imprecise.

