Inspiron
Inspiron

Reputation: 51

Trying to access Parent and then the sibling element in selenium. Throws no element exception

Element structure

//table[@class='table-table-condensed']//tbody//tr...
  <td class="ng-binding">TEXT1</td>
  <td class="ng-binding">2020-04-17 18:55:58.022</td>
  <td>
    <span class="label label-default">Not Started</span>
  </td>
  <td>
    <div class="pull-right">
      <button class="btn-success"/>
      <button class="run"/>
      <button class="review"/>
    </div>
  </td>

Trying to access the button element :

If Text Matches for this Element :

WebElement we = driver.findElement.By(xpath("//table[@class='table-table-condensed']//tbody//tr//td[@class='ng-binding'][1]");
String str = we.text();
if(str.equals("TEXT1"))
{
    WebElement ex = we.findElement(By.xpath("./parent::following:://button[@class='run']"));

    ex.click();
}

this throws no element found exception, Tried following::sibling too. Can we use both the tags at a time here. How to access the button element.

Also, will following:: tag does not work, if I had to access the below tags in structure:

<td class ="ng-binding">2020-04-17 18:55:58.022</td>   ( Need to extract this element to sort the list by Latest date)

AND

<span class ="label label-default">Not Started</span>  

Upvotes: 0

Views: 79

Answers (3)

SiKing
SiKing

Reputation: 10329

Your ./parent::following:://button[@class='run'] is not valid XPath!

The correct form is: axis::node[predicate]. You are not allowed to chain multiple axis together, and you must specify a node! Note that a wilcard * node is still valid.

What you are probably looking for is ./following-sibling::td//button[@class='run'] or possibly ./following::button[@class='run'].

Upvotes: 1

SeleniumUser
SeleniumUser

Reputation: 4177

Try below solution

wait = WebDriverWait(driver, 10)    
elementText= wait.until(EC.element_to_be_clickable((By.XPATH, "//table[@class='table-table-condensed']//tbody//tr/following-sibling::td[contains(text(),'Test1')][contains(@id, "ng-binding")]")))
button=wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@class='run']")))
if(elementText.text=="Test1"): 
   button.click()
else:
   print "Not found!"

Upvotes: 2

frianH
frianH

Reputation: 7563

Other approach, you can try to solve this by using tr[contains(., 'TEXT VALUE')]:

String text = "TEXT1";
WebElement element1 = driver.findElement(By.xpath("//table[@class='table-table-condensed']//tr[contains(., '" +text +"')]//td[2]"));
WebElement element2 = driver.findElement(By.xpath("//table[@class='table-table-condensed']//tr[contains(., '" +text +"')]//span"));
WebElement element3 = driver.findElement(By.xpath("//table[@class='table-table-condensed']//tr[contains(., '" +text +"')]//button[@class='run']"));
System.out.println(element1.getText());
System.out.println(element2.getText());
element3.click();

Upvotes: 1

Related Questions