I am trying to select the second option from the div drop down via python/selenium. The code below is what I have so far. The first line opens the drop down and works. The second line attempts to select the 2 option and click it. I have tried "2", "22", and "Option-22" but none seem to work.
Sample Python Selenium Code
#opens drop down
browser.find_element(By.XPATH,".//*[@id='Account']").click()
#selects item from dropdown
browser.find_element(By.XPATH,".//*[@id='Account']/option[22]").click()
Error Message:
Message: invalid selector: Unable to locate an element with the xpath expression .//*[@id='Account']/option[22] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string './/*[@id='Account']/option[22]' is not a valid XPath expression.
Sample HTML Code:
<div id="Account" >
<select>
<option selected="" value="11">Option-11</option>
<option value="22">Option-22</option>
<option value="33">Option-33</option>
</select>
</div>
CodePudding user response:
The XPath you want is:
"//*[@id='Account']//option[@value='22']"
Or you can use the following CSS Selector:
'#Account option[value="22"]'
Or with SeleniumBase, you can do the full select option in one line:
self.select_option_by_text("#Account select", "Option-22")
(Full disclosure: I maintain SeleniumBase)