0

I want to know how can I select the option from dropdown list from website.

The html is here

<thead> <td style="width: 40%;"> <select name="product_size" id="sct-size" data-md-selectize> <option value="-">Choose Size</option> <option value="323">XS</option> <option value="324">S</option> <option value="325">M</option> <option value="326">L</option> <option value="327">XL</option> <option value="328">XXL</option> <option value="342">1 years old</option> <option value="343">5 years old</option> <option value="344">8 years old</option> <option value="345">12 years old</option> </select> </td> <td style="width: 40%;"> <select name="product_color" id="sct-color" data-md-selectize> <option value="-">Choose Color</option> <option value="594">N/A</option> </select> </td> <td style="width: 19%;"><input type="text" class="md-input" name="product_stock_" id="inp-stock" placeholder="Stock" style="margin-top: -11px;text-align: center;"/></td> <td style="width: 1%;"><a href="#" id="btn-addstock" style="margin-top: 5px; display: block;" title="Add Stock"><i class="material-icons">&#xE148;</i></a></td> </thead> 

I want to select the "Choose Size" then "S" option. I tried this code.

from selenium import webdriver from selenium.webdriver.support.select import Select mySelect = Select(driver.find_element_by_id("sct-size")) mySelect.select_by_visible_text("S") 

But got the error

NoSuchElementException: Message: Could not locate element with visible text: S

I already looked many solution regard to this problem here on stackoverflow. Its suppose to select the "S" from dropdown menu but didn't. I don't know what else I can try.

Thanks.

already tried

  1. https://sqa.stackexchange.com/questions/12029/how-do-i-work-with-dropdowns-in-selenium-webdriver
  2. Select a dropdown using Python + Selenium

Edited 1

I also tried

mySelect = Select(driver.find_element_by_id("sct-size")) mySelect.select_by_value("323") 

but got this error

NoSuchElementException: Message: Cannot locate option with value: 323

Also I tried to change the find_element method with name

obj = Select(driver.find_element_by_name('product_size')) obj.select_by_index(1) 

and got error

NoSuchElementException: Message: Cannot locate option with index 1

Perhaps the find_element is the problem? I don't know.

Edited 2

I tired to play around with 'inspect' tool in google chrome and found that when the page load, the html only load 1 option (which in this case, its 'Choose Size'). This explain why the error.

html code when the page load.

<thead> <td style="width: 40%;"> <select name="product_size" id="sct-size" data-md-selectize> <option value="-">Choose Size</option> </td> <td style="width: 40%;"> <select name="product_color" id="sct-color" data-md-selectize> <option value="-">Choose Color</option> </select> </td> <td style="width: 19%;"><input type="text" class="md-input" name="product_stock_" id="inp-stock" placeholder="Stock" style="margin-top: -11px;text-align: center;"/></td> <td style="width: 1%;"><a href="#" id="btn-addstock" style="margin-top: 5px; display: block;" title="Add Stock"><i class="material-icons">&#xE148;</i></a></td> </thead> 

Tried Seema Nair solution but got other error

AttributeError: 'Select' object has no attribute 'click'

my new code is

mySelect = Select(driver.find_element_by_id("sct-size")) mySelect.click() mySelect.select_by_visible_text("S") 

So, new question, how can i click dropdown to let the option code reveal?

    1 Answer 1

    1

    Your original code selecting by visible text or value will work.

    I faced a similar issue and this was because, before selecting the value from the drop down, I had to do a click on the dropdown link and then perform the select.

    So add a click method before the Select and the same code you had initially will work.

    5
    • So, i tried but got another error. AttributeError: 'Select' object has no attribute 'click'..CommentedFeb 1, 2019 at 5:41
    • mySelect = Select(driver.find_element_by_id("sct-size")) mySelect.click() mySelect.select_by_visible_text("S")CommentedFeb 1, 2019 at 5:41
    • i update my question based on what you suggest on 'Edited 2'CommentedFeb 1, 2019 at 7:54
    • 1
      Just try to adjust my code and you right. Thank a lot.CommentedFeb 1, 2019 at 8:47
    • I am glad, i could help. Good luck!CommentedFeb 1, 2019 at 18:18

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.