Issue
This is my very first Stack Overflow post so please take it easy on me!
I am using Selenium to fill in a webform that contains various dropdowns. I attempted to accomplish this by creating a function that finds the elements, and selects the value by using the "select_by_visible_text"
functionality.
I created two lists, one list contains elements, and the other list contains the values that need to be selected in each dropdown.
Below is the function that I attempted to use:
def dropdown_select(element_list, selection_list):
for element in element_list:
find_element = driver.find_element(By.ID, element)
find_element.click()
for selection in selection_list:
select = Select(find_element)
select.select_by_visible_text(selection)
The problem is that after it selects the first dropdown correctly, it cannot find the value for the second dropdown selection which raises the following error:
selenium.common.exceptions.NoSuchElementException: Message: Could not locate element with visible text: Semester Units
I spent the last few days making sure that the correct Element ID's and correct Visible Text is in the list. As you can see on the error above, the script has trouble finding the value. I have inspected the element multiple times to make sure it was the correct text but I still get the same error.
Additionally, I tried selecting the dropdown option by using select_by_value
but I still receive the same error.
I have also tried using an implicit wait but it still did not find the value.
Solution
The problem is that your function tries to apply all selections from selection_list to each element in element_list, instead of matching each element with its corresponding selection. You should iterate over both lists simultaneously using zip.
Try this instead:
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
def dropdown_select(element_list, selection_list):
for element, selection in zip(element_list, selection_list):
select_element = Select(driver.find_element(By.ID, element))
select_element.select_by_visible_text(selection)
Source: My article https://ioflood.com/blog/python-zip-function/
Answered By - Gabriel Ramuglia
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.