我宁愿避免使用xpath,除非这确实是唯一的方法。 下面是我正在使用的简单复选框:w3schools.checkbox
我想查一下“我有辆自行车”。我尝试在self.driver
上调用find_element_by_name
方法,但是没有成功,下面是我使用xpath的痛苦尝试:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
class CheckBox:
def __init__(self):
self.url = 'https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_input_checked'
self.driver = webdriver.Firefox()
self.driver.get(self.url)
def check_I_have_a_bike(self):
bike_xpath = ".//input[@value='Bike']" # seems pretty straightforward and simple
try:
self.driver.find_element_by_xpath(bike_xpath).click()
except NoSuchElementException as e:
print('Error: {error_message}'.format(error_message=e))
checker = CheckBox()
checker.check_I_have_a_bike()
`Error: Unable to locate element: .//input[@value='Bike']`
我做错了什么?在
位于
iframe
内的目标input
字段。要处理复选框,必须先切换到iframe
:它也应该可以通过
^{pr2}$name
访问:但请注意,名称
"vehicle"
应用于这两个复选框您可能还需要使用
从
iframe
返回相关问题 更多 >
编程相关推荐