![](/img/trans.png)
[英]How to extract the text of the selected option of a Dropdown Menu using Selenium and Python
[英]Selenium python trying to extract the first selected option value from the dropdown
我正在尝试从下拉列表中提取第一个选定的值。 不知道我在这里错了
到目前为止,这是我尝试过的:
currency = Select(self.driver.find_element_by_id("currencyid"))
currency.select_by_visible_text("AUD")
assert currency.first_selected_option().text == "AUD"
HTML代码如下所示:
这是我得到的错误,不确定这有多有用:
File "Test.py", line 54, in test_TestOne
assert currency.first_selected_option().text == "USD"
File "C:\Python27\lib\site-packages\selenium\webdriver\support\select.py", line 62, in first_selected_option
for opt in self.options:
File "C:\Python27\lib\site-packages\selenium\webdriver\support\select.py", line 47, in options
return self._el.find_elements(By.TAG_NAME, 'option')
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 528, in find_elements
{"using": by, "value": value})['value']
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 494, in _execute
return self._parent.execute(command, params)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=56.0.2924.87)
(Driver info: chromedriver=2.25.426923 (),platform=Windows NT 10.0.10586 x86_64)
currency = Select(self.driver.find_element_by_id("currencyid"))
currency.select_by_visible_text("AUD")
assert currency.first_selected_option.text == "AUD"
您应该使用first_selected_option
而不是first_selected_option()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.