'A way of selecting the next xpath element
How can i switch to the next element from a table using python and selenium. I have a website with a table that looks like this: https://i.stack.imgur.com/ZYwKU.png I managed to identify and click on the element from the first column. Next, i want to select the element from second column which can have any value instead of "true".
I tried using Xpath Axes but i did not succed:
#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()
To be applied to the following xhtml fragment (contents of the style attribute intentionally omitted):
<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>
Solution 1:[1]
The following xpath matches the sibling nodes carrying the data-cell-index
attibute.
//div[@data-cell-index]/following-sibling::*[@data-cell-index]
Attributes are referenced by an @
prefixed to their names. An axis like following-sibling
asks for an element spec. Here it is the wildcard *
but you could use div
according to your fragment.
From your description you might actually want to change your code as follows:
find_elem.find_elements (By.XPATH, "./(following-sibling::*[@data-cell-index])[2]").click()
Your target node is not the immediate sibling successor carrying the data-cell-index
attribute but the one after.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |