1

I can't figure it out how to find xpath using following-sibling for those marked in green in the pic. Basically I want to click on the button called "Open" which is on the same row as the text "front".

enter image description here

3
  • 1
    //strong[text()='front']/following::a[contains(text(),"Open"][1] - slightly off what you're asking, this will just pick the first a with text Open after your front without it being a sibling (but might cause error and pick the NEXT open if that line doesn't have one) - if you can paste the html as text (not an image!) i'll look a bit deeper Sep 1, 2020 at 15:04
  • Or just use jaSON's answer! :-) Sep 1, 2020 at 15:11
  • 2
    Does this answer your question? XPath locate child following sibling
    – rahul rai
    Sep 1, 2020 at 16:00

4 Answers 4

2

Try this XPath:

//td[strong="front"]/following-sibling::td//a[.="Open"]

P.S. As you didn't provide HTML sample as text (do not provide it as picture!!!) I can't see whether there is a text in <i>...</i> node

So you also can try

//td[strong="front"]/following-sibling::td//a[contains(., "Open")]
0
1

Try this

//strong/parent::td/following- sibling::td/descendant::i/following-sibling::text() [contains(.,'Open')]

The above xpath is assuming the level of Open text and i is of same level

1

You can use this XPath:

//td[strong="front"]/following-sibling::td//a[contains(text(), "Open")]
1

As per the HTML you have shared the element with text as front doesn't have any siblings. So you may not be able to use following-sibling.


Solution

To click on the button with text as Open which is on the same row with the element with text as front you you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following based Locator Strategies:

  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//strong[text()='front']//following::td[1]//a[@class='btn default ']"))).click()
    
  • Using XPATH along with the text:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//strong[text()='front']//following::td[1]//a[contains(., 'Open')]"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.