选择用 <a> 和 <i> 标签包裹的复选框?

问题描述 投票:0回答:1

我正在尝试使用

is_selected()
方法检查是否选中了该复选框。我知道,由于我尝试验证的元素不是合法的复选框,因此以这种方式验证它是不正确的。它非常明显,因为它被包裹在锚点(
<a>
)和斜体标签(
<i>
)中。我尝试过 XPath 的不同变体,例如:

//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。如何让它识别该复选框已被选中?

html xml selenium-webdriver xpath checkbox
1个回答
0
投票

这个 XPath,

//input[@name='non_stop']

将选择文档中任意位置且

input
@name
的所有
non_stop
元素,即使包裹在另一个元素(例如
a
i
)中。

直接环绕的元素称为 parent 元素,可以通过

parent::*
..
:

选择
//input[@name='non_stop']/parent::*
//input[@name='non_stop']/..

另请参阅

© www.soinside.com 2019 - 2024. All rights reserved.