2

Hi I am trying to use selenium to select an item from a dropdown using the actual text not the value option. My script pulls states out of a list and iterates through the states selecting the dropdown that matches the list. When I try the following code it throws out an error:

for agentinfo1, agentstate1 in zip(agentinfo, agentstate):
    select2 = driver.find_element_by_css_selector("#selst" %agentstate1)
    select2.click()

select2 = driver.find_element_by_css_selector("#selst" %agentstate1) TypeError: not all arguments converted during string formatting

I'm wondering if the error is thrown out becuase when I grab the data that I put in my list I append a "\n" but even when I take out that code it does not work.

1 Answer 1

2

you are using css IDs instead of CSS classes. select2 is pointer to the select element, if your text is exactly the same as in your variable

from selenium.webdriver.support.ui import Select

for agentinfo1, agentstate1 in zip(agentinfo, agentstate):
    select2 = Select(self.driver.find_element_by_id("<some id of the select element>")).select_by_visible_text(agentstate)

if it is only partial match you can try this (case insensitive partial match):

from selenium.webdriver.support.ui import Select

for agentinfo1, agentstate1 in zip(agentinfo, agentstate):
    select2 = Select(self.driver.find_element_by_id("<some id of the select element>"))
    for each_option in select2.options:
        if agentstate.lower in each_option.text.lower:
            select2.select_by_index(int(each_option.get_attribute("value")))
Sign up to request clarification or add additional context in comments.

1 Comment

Minor typo: it should be agentstate.lower() instead of agentstate.lower. Also, casefold() can be a better approach in case of multiple languages involved

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.