1

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

1
  • we need more information from you. What's the website for example? Commented Aug 25, 2016 at 0:21

2 Answers 2

3

If you are getting NoSuchElementException as your provided exception, There may be following reasons :-

  • May be you are locating with incorrect locator, So you need to share HTML for better locator solution.

  • May be when you are going to find element, it would not be present on the DOM, So you should implement WebDriverWait to wait until element visible as below :-

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    wait = WebDriverWait(driver, 10)
    
    element = wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='posted_1']/div[3]")))
    
  • May be this element is inside any frame or iframe. If it is, you need to switch that frame or iframe before finding the element as below :-

    driver.switch_to_frame("frame/iframe I'd or name")
    
    wait = WebDriverWait(driver, 10)
    
    element = wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='posted_1']/div[3]")))
    
    #Once all your stuff done with this frame need to switch back to default
    driver.switch_to_default_content();
    
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks Saurabh, but after implementing your suggestion this is what I get to see in the output. 0.00s [INFO] Starting measurement [160824_FW_3QJW] 0.36s [INFO] Executing WebDriver script 20.76s [ERROR] TimeoutException: Traceback (most recent call last): Line 34, in <synthetic script> element = wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='posted_1']/div[3]"))) Line 80, in until raise TimeoutException(message, screen, stacktrace)
If your xpath is correct then make sure this element is not inside any frame or iframe... if it is then consider third provided point...:)
Hi Saurbah, yes i checked the xpath. Just to be safe - tried the find element by name for the same attribute but still getting the same error.This is the actual element. <input name="searchBox" placeholder="What are you looking for?" type="text" ng-model="search.keyword" on-enter-key="doSuperSearch(search.keyword)" ng-blur="onSearchBlur()" class="ng-pristine ng-valid ng-empty ng-touched" style="">
This is what I put in my script. wait = driver.implicitly_wait(10) element = wait.until(EC.visibility_of_element_located((By.NAME, "searchBox")))
This is the output I got. 0.00s [INFO] Starting measurement [160825_5F_11V] 0.38s [INFO] Executing WebDriver script 11.32s [ERROR] AttributeError: 'NoneType' object has no attribute 'until' Traceback (most recent call last): Line 34, in <synthetic script> element = wait.until(EC.visibility_of_element_located((By.NAME, "searchBox")))
|
0

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.

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.