选择下一个xpath元素的方法



如何使用python和selenium切换到表中的下一个元素?我有一个网站的表,看起来像这样:https://i.stack.imgur.com/ZYwKU.png我成功地识别并点击了第一列中的元素。接下来,我想从第二列中选择可以有任何值的元素,而不是"true"

我尝试使用Xpath轴,但没有成功:

#find element 
find_elem = wait.until(EC.presence_of_element_located((By.XPATH, "//*[contains(text(), 
'httpGzipActive')]")))
time.sleep(2)
find_elem.click()
time.sleep(2)
#here im trying to switch to the next element, which is Value column
driver.find_element(By.XPATH, "//div[* = 'data-cell-index']/following-sibling::data- 
cell-index").click()

应用于以下xhtml片段(有意省略style属性的内容):

<div data-cell-index="0" style="..."></div>
<div data-cell-index="0" style="...">httpGzipActive</div>
<div data-cell-index="1" style="..."></div>
<div data-cell-index="1" style="...">true</div>

下面的xpath匹配携带data-cell-index属性的兄弟节点。

//div[@data-cell-index]/following-sibling::*[@data-cell-index]

属性由其名称前缀@引用。像following-sibling这样的轴要求元素规范。这里是通配符*,但您可以根据您的片段使用div

从你的描述,你可能实际上想改变你的代码如下:

find_elem.find_elements (By.XPATH, "./(following-sibling::*[@data-cell-index])[2]").click()

您的目标节点不是携带data-cell-index属性的直接兄弟继承节点,而是其后的节点。

相关内容

最新更新