1

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.

asked Apr 13, 2014 at 16:21
5
  • What is stored inside id[2]? If it has \' instead of ' then removing the \ might solve your problem. Commented Apr 13, 2014 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 /' Commented Apr 14, 2014 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. Commented Apr 14, 2014 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 Commented Apr 14, 2014 at 11:03
  • Alright, will do Commented Apr 15, 2014 at 12:37

2 Answers 2

2

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.

answered Apr 14, 2014 at 14:22
0

I tried a lot many things, but my drop down was inside a table and I was not able to perform a simple select operation. Only the below solution worked. Here I am highlighting drop down elem and pressing down arrow until getting the desired value -

 #identify the drop down element
 elem = browser.find_element_by_name(objectVal)
 for option in elem.find_elements_by_tag_name('option'):
 if option.text == value:
 break
 else:
 ARROW_DOWN = u'\ue015'
 elem.send_keys(ARROW_DOWN)
answered Nov 24, 2016 at 6:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.