Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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

<select style="visibility: visible;" name="sex" id="sex" onchange="changefees()">
<option value="0">Select Gender</option>
<option value="1">Male</option><option value="2">Female</option>
</select>

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.

share|improve this question
    
What is stored inside id[2]? If it has \' instead of ' then removing the \ might solve your problem. –  Dean Barnes Apr 13 at 20:32
    
id[2] contains a string "sex", or any other string value such as "date" which is the second field i tried. The error message if from when i tried to use xpath and used '' and selenium autoescaped it. The find by id does not return the /' –  Aayush Agrawal Apr 14 at 4:17
    
I managed to solve the problem myself, turns out the problem was not in the above snippet of code at all. The driver instance being supplied to the code was wrong. –  Aayush Agrawal Apr 14 at 4:27
    
Could you please post as an answer that you had the driver instance incorrect, and then mark your answer as the correct one? It would help others if you added information on how you determined your driver instance was incorrect –  Kate Paulk Apr 14 at 11:03
    
Alright, will do –  Aayush Agrawal Apr 15 at 12:37
add comment

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.