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

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
bbfl
  • 171
  • 8
  • `//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 – RichEdwards Sep 01 '20 at 15:04
  • Or just use jaSON's answer! :-) – RichEdwards Sep 01 '20 at 15:11
  • 2
    Does this answer your question? [XPath locate child following sibling](https://stackoverflow.com/questions/31835893/xpath-locate-child-following-sibling) – rahul rai Sep 01 '20 at 16:00

4 Answers4

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")]
JaSON
  • 3,460
  • 2
  • 6
  • 13
1

You can use this XPath:

//td[strong="front"]/following-sibling::td//a[contains(text(), "Open")]
JaSON
  • 3,460
  • 2
  • 6
  • 13
Ahmed Mamdouh
  • 683
  • 1
  • 11
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

nikhil udgirkar
  • 181
  • 1
  • 3
  • 20
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
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217