简体   繁体   中英

Acquiring drop down menu option value selecting through name

I am new to python and I'm trying to understand how to click on a dropdown menu where I select it through the name and not the value even though the name is shown multiple times throughout the code because each shirt has a different value where a jacket can have 14123 and a shirt has 14133 as the value.

Here is My Code:

browser = webdriver.Chrome()

Size=browser.find_element_by_xpath("//select[@name='X-Large']/option[@value='12218866729085']").click()

HTML Code EX for one shirt:

select id="product-select" name="id" class="">

option value="12218866630781">Small</option>
option value="12218866663549">Medium</option>
option value="12218866696317">Large</option>
option value="12218866729085">X-Large</option>

Gives this error:

("//select[@name='X-Large']/option[@value ='12218866729085']").click() AttributeError: 'str' object has no attribute 'click'

你可以使用这个 xpath - //select/option[text()='Medium']

Its a select dropdown so you need to use select command to choose values from dropdown. for example in java

  Select dropdown = new Select(driver.findElement(By.id("product-select")));

  dropdown.selectByVisibleText("Medium"); //to select medium

instead of visible text we can use index or value, like

 dropdown.selectByIndex(1); //second option in dropdown as count starts from 0 in java

or

 dropdown.selectByValue("12218866663549"); //using value attribute

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM