selenium is_selected()用法
在Selenium中,`is_selected()` 方法用于检查一个选项是否被选中。这个方法通常用于检查下拉列表(select)中的选项是否被选中。
以下是使用 `is_selected()` 方法的示例代码:
```python
from selenium import webdriver
启动浏览器并打开网页
driver = ()
("
选择下拉列表元素
select_element = _element_by_xpath("//select[id='my_select']")
获取选项元素
option_element = _element_by_xpath("//select[id='my_select']/option[text()='Option 1']")
检查选项是否被选中
if option__selected():
    print("Option 1 is selected")
else:
    print("Option 1 is not selected")
切换到其他选项
option_element2 = _element_by_xpath("//select[id='my_select']/option[text()='Option 2']")
option_()
检查新选项是否被选中
if option__selected():
    print("Option 2 is selected")
else:
    print("Option 2 is not selected")
```selenium中xpath定位
在上面的示例中,我们首先启动了Chrome浏览器并打开了指定的网页。然后,我们使用XPath定位了下拉列表元素和选项元素。通过调用 `is_selected()` 方法,我们可以检查选项是否被选中。如果选项被选中,将输出相应的消息;否则,将输出另一条消息。最后,我们切换到另一个选项并再次检查其状态。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。