1

How can I select a dropdown element by a part of its name? I want to select an option based on a DB values, but this values don't have the complete name of the dropdown element, is there any way to make selenium look for the option with my database value as a partial text?

    modelo = googleSheet.modelo.upper().strip()
    select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div/div/div/div[1]/form/fieldset[6]/div/ul/fieldset[3]/div/ul/fieldset[3]/div/ul/fieldset/div/ul/li/label'))))
    select.select_by_visible_text(modelo)

I the dropdown option I want to select is "Terrano II 2.7 xpto ol", but my data base value is just Terrano II 2.7

Thanks for the help

Tooc
  • 13
  • 1
  • 7
  • 1
    First thing is select only works with the Select tag. Next is you can use "//[contains(.,'{}')]".format("Terrano ll") to select options with that value. Or starts-with – Arundeep Chohan Mar 31 '21 at 17:58
  • This is correct. 'Select' will work only with only with 'select' tag and 'option' tag as options in dropdown. – vitaliis Apr 01 '21 at 17:39

2 Answers2

0

What about if first you extract the dropdown text content and then you check if your db query is in text? something like this:

Selenium Select - Selecting dropdown option by part of the text

Giovanni Frison
  • 628
  • 3
  • 19
  • I didn't downvote you. I am just trying to make the code work cause it's python and the solution is not, but thanks for the answer. I think it is going somewhere :) – Tooc Mar 31 '21 at 10:58
  • yes I know it is not python but most of the selenium command are the same (or almost). Therefore, I thought you could start from there.. – Giovanni Frison Mar 31 '21 at 11:05
0

driver.select_by_visible_text() already does strip(). You do not need it. Also, from this method definition:

Select all options that display text matching the argument. That is, when given "Bar" this would select an option like:
<option value="foo">Bar</option>
:Args:
 - text - The visible text to match against

So, you need to expect exactly the option that is visible. Another problem in your code is the way you pass the variable.

dropdown_option = "Some text you expect to see in the dropdown"
locator = driver.find_element_by_id("id")  # or any other locator
select = Select(locator)
        if locator is not None:
            for option in select.options:
                select.select_by_visible_text(dropdown_option) 

This implementation makes debugging easier. For example you can print all the values from your dropdown before selecting the option you need.

If it takes a lot of time to dropdown to open, or other elements make your dropdown temporarily not visible, add a separate wait before selection.

from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By

 wait = WebDriverWait(driver, 10)
        wait.until(EC.visibility_of_element_located(
            (By.CSS_SELECTOR, "Unique css selector of the first option in dropdown")))
vitaliis
  • 4,082
  • 5
  • 18
  • 40