1

I was testing my code over: https://semantic-ui.com/modules/dropdown.html

I wrote the following code in Python3 which finds all drop-downs in a given webpage and selects the last option for each drop-down:

dropdowns = driver.find_elements(By.XPATH, '//select[@class="ui dropdown"]')
print(str(len(form_dropdowns)))
for drop_down in form_dropdowns:
    driver.execute_script("arguments[0].click();", drop_down)
    options = drop_down.find_elements(by=By.TAG_NAME, value='option')
    print(str(len(options)))
    driver.execute_script("arguments[0].click();", options[len(options) - 1])
time.sleep(100)

I see the following printed on screen:

2
3
3

Which means 2 drop-downs were found, both with 3 options to select from.

BUT I don't see any value being changed in the UI, why is that?

Algo
  • 23
  • 4
  • This sounds like an [X-Y problem](http://xyproblem.info/). Instead of asking for help with your solution to the problem, edit your question and ask about the actual problem. What are you trying to do? – undetected Selenium Mar 25 '22 at 19:52
  • I'm still not sure what you are doing through `print(str(len(form_dropdowns)))` and `for drop_down in form_dropdowns` where as you got `dropdowns = driver.find_elements(By.XPATH, '//select[@class="ui dropdown"]')` – undetected Selenium Mar 25 '22 at 21:21
  • The elements are not visible also it seems there is a div that actually does the genders. – Arundeep Chohan Mar 25 '22 at 21:23

1 Answers1

1

Although it seems OP's approach to select the last option using the following line of code is working,

 driver.execute_script("arguments[0].click();", options[len(options) - 1])

I'm totally against the idea of selecting the last option.

Ideally to interact with any element we must be using the Select() class and use either among the option index, option value or option text to choose the desired <option>.

As an example,

  • Using select_by_value():

    Select(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "xpath_select_element")))).select_by_value("value")
    
  • Using select_by_visible_text():

    Select(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "xpath_select_element")))).select_by_visible_text("text")
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import Select
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you so much for helping, for some reason I got blocked and can't test that website I will test and accept very soon. Just a side note, isn't really there an option to select last one? I don't know the name as I'm writing general purpose code that is supposed to work on almost all scenarios. – Algo Mar 25 '22 at 23:12
  • Update: It's not working. – Algo Mar 25 '22 at 23:33
  • Thanks for your effort but I won't accept as my problem is still unresolved. – Algo Mar 27 '22 at 00:40
  • I'm still not sure with which select element(s) you are trying to interact. However its a generic solution. You need to replace _`xpath_select_element`_ with a realtime xpath. – undetected Selenium Mar 27 '22 at 09:06