Unable to find element in selenium



  • I'm using the following code:

    if i[1] == "id_text":
        inst = driver.find_element_by_id(i[2])
        #inst.click()
        for option in inst.find_elements_by_tag_name('option'):
            if option.text == i[3]:
                option.click()
    

    But it keeps constantly raising the error

    raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"xpath","selector":"//select[@id=\'sex\']"}' ; Stacktrace:

    I know the if statement is being executed, and that the value of the id is correct. To try to fix this i tried replacing id with name, no result. I then tried using find_element_by_id and find_element_by_name, still unable to find the element.

    The element does exist and the page has loaded. I know this for certain because before the dropdown there exists another text field which is filled in properly, there is no ajax on the website either but i did try using waits. 60 sec waits didn't help either.

    The dropdown i am trying to zero in on is

    
    
    
    
    

    The value of the id/name being supplied as well as the text is correct, i verified that by printing the values before executing the code. Any help is highly appreciated.

    Edit: I also tried to use the same code on another dropdown on the page, same result.



  • It turns out i was able to find the problem by myself, it was not at all in the above snippet of code.

    if i[1] == "id_text":
        inst = driver.find_element_by_id(i[2])
        #inst.click()
        for option in inst.find_elements_by_tag_name('option'):
            if option.text == i[3]:
                option.click()
    

    This above code was inside of a function instructions(webdriver, i) however in the dropdown selector i used the variable 'driver' instead of 'webdriver'. This did not give a driver not found error as i was juggling multiple driver instances and there was a driver instance named driver but it was on another page, hence throwing the element not found error.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2