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"></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
- https://sqa.stackexchange.com/questions/12029/how-do-i-work-with-dropdowns-in-selenium-webdriver
- 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"></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?