Find the next related element in Python Selenium?

I have this HTML:

<body>
    <p id='one'> 1A </p>
    <p id='two'> 2A </p>
    <p id='three'> 3A </p>
    <p id='four'> 4A </p>
    <p id='five'> 5A </p>
    <p id='six'> 6A </p>
    <p id='seven'> 7A </p>
</body>

I use the code below to get the first item with Selenium:

elem = driver.find_element_by_id('one')

Now, how can I find the next brother to find the next item?

+4
source share
3 answers

Using Xpath:

driver.find_element_by_xpath("//p[@id, 'one']/following-sibling::p")
+6
source

I want to correct the answer of Mark Rowlands. The correct syntax will be

driver.find_element_by_xpath("//p[@id='one']/following-sibling::p")
+5
source

Javascript , -

document.getElementById("one");

jQuery -

$("#one");
-7

Source: https://habr.com/ru/post/1542168/


All Articles