bbfl
bbfl

Reputation: 365

Following-sibling in XPath using Python and Selenium

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

Upvotes: 1

Views: 1644

Answers (4)

undetected Selenium
undetected Selenium

Reputation: 193058

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
    

Upvotes: 1

Ahmed Mamdouh
Ahmed Mamdouh

Reputation: 706

You can use this XPath:

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

Upvotes: 1

nikhil udgirkar
nikhil udgirkar

Reputation: 357

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

Upvotes: 1

JaSON
JaSON

Reputation: 4869

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")]

Upvotes: 2

Related Questions