0

I have a web process that I am working through to automate. The HTML looks as following:

<td class="v-formlayout-contentcell">
<div class="v-select v-widget v-has-width" id="gwt-uid-46" aria-labelledby="gwt-uid-45" style="width: 225px;">
 <select class="v-select-select" size="1" tabindex="0" style="width: 225px;">
 <option value="null"></option><option value="1">21/24 Long Term Auction Credit Study</option><option value="2">21/24 Long Term Auction</option></select></div></td>

I always know what my options are from the dropdown list.

If I know what option needs to be selected, is there a way to select that based on text of option. In this example I want to select 21/24 Long Term Auction Credit Study.

I cant use id = "gwt-uid-46" as I fear that might easily change.

Edit: I noticed in the the HTML that there is another class with name as class="v-select-select". So, none of the answers posted so far work as the it finds that class instead of the one I am interested in.

Zanam
  • 4,607
  • 13
  • 67
  • 143

2 Answers2

0

from selenium.webdriver.support.ui import Select

select = Select(WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.Class_name,"v-select-select"))
select.select_by_visible_text("21/24 Long Term Auction Credit Study")
or 
select.select_by_value("1")
Amruta
  • 1,128
  • 1
  • 9
  • 19
  • I get the error: selenium.common.exceptions.NoSuchElementException: Message: Could not locate element with visible text: 21/24 Long Term Auction Credit Study. The second option gives error: TypeError: argument of type 'int' is not iterable – Zanam May 28 '20 at 03:21
  • I have updated my answer try to add wait and for second option i forgot to add double quotes – Amruta May 28 '20 at 03:47
  • And also check if its in iframe or it needs click event before showing up all dropdown options – Amruta May 28 '20 at 03:57
0

To select the <option> with text as 21/24 Long Term Auction Credit Study using Selenium and from the you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:

  • CSS_SELECTOR:

    select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "select.v-select-select"))))
    select.select_by_visible_text("21/24 Long Term Auction Credit Study")
    
  • XPATH:

    select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@class='v-select-select']"))))
    select.select_by_visible_text("21/24 Long Term Auction Credit Study")
    
  • Note : You have to add the following imports :

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

Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352