1

I'm trying to automate some admin tasks at work, so I'm using selenium to manipulate our project management platform (quickbase).

The first item I populate is a select/options drop down, which I can select a record by using the xpath selector:

driver.find_element_by_xpath("//select[@id='_fid_67']/option[@value='28']").click()

This works just fine. The next item I need to populate is also a select/options drop down. The available options are queried by the first selected option. In firebug it looks like they are calling the function to query the results on click of the 2nd drop down. So next I tried just clicking the select element id, which should call the function to query the options. Then I let it wait (tried for as long as 10 seconds), and select the option.

# click the select element to call function
driver.find_element_by_xpath("//select[@id='_fid_74']").click()

# wait for the query function to finish
driver.implicitly_wait(2)

# select the option
driver.find_element_by_xpath("//select[@id='_fid_74']/option[@value='142']").click()

This gives me one of two errors:

Element not found in the cache - perhaps the page has changed since it was looked up

or

File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 107, in check_response
    message = value["value"]["message"]
TypeError: string indices must be integers

I also tried returning a list of elements, looping through them and checking if the value '142' and if so click that element object. That gave me an error as well. I've tried using xpath: //select[@id='_fid_74']/option[text()='the text'] which didn't work either.

I assume it has something to do with the conditional selection, since the same query worked for the first dropdown (first code block).

Anyone have any suggestions?

Tyler
  • 201
  • 2
  • 6

2 Answers2

2

First of all, you should use the Select class that makes dealing with select and it's options quite simple and abstracts away the complexity:

from selenium.webdriver.support.select import Select

select = Select(driver.find_element_by_xpath("//select[@id='_fid_74']"))
select.select_by_value("142")

string indices must be integers

This is a known issue in selenium 2.49. Currently, you need to downgrade to 2.48:

pip install selenium==2.48 
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

Instead of implicit wait,use explicit wait as shown below:

from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
element=wait.until(EC.element_to_be_selected((driver.find_element_by_xpath("//select[@id='_fid_74']/option[@value='142']"))))