我正在try 使用is_selected()
方法判断是否 Select 了复选框.我明白,由于我试图验证的元素不是合法复选框,因此以这种方式验证它是不正确的.这非常明显,因为它被锚定(<a>
)和意大利标记(<i>
)Package.我try 过不同的Xths变体,例如:
//input[@name='non_stop']/preceding-sibling::i
//input[@name='non_stop']/parent::a
//a[@title='Non Stop Flights']/i[contains(@class, 'ico ico-checkbox')] (possibly the most wrong one here)
但有解决办法来验证这一点吗?这是我的代码:
driver = webdriver.Chrome()
driver.get("https://www.yatra.com/")
non_stop_flights = driver.find_element(
By.XPATH, "//input[@name='non_stop']/preceding-sibling::i")
print("Before clicking")
print(non_stop_flights.is_selected()) # Expecting False here
non_stop_flights.click()
sleep(3)
print("After clicking")
print(non_stop_flights.is_selected()) # Expecting True here
但我仍然得到最后一行的输出为False.如何让它识别出复选框已被选中?