0

This question is an exact duplicate of:

I want to locate element 'TableHeader' in given DOM, I am using XPath but getting an exception unable to locate the element.

Xpath : //div[@class='PageContent']/table[1]/tbody/tr/td[2]

Please have a look if there is anything wrong in my XPath:

DOM:

<form> 
  <table id="topNavigation" cellspacing="0" cellpadding="0" border="0" width="100%"> 
    <div class="PageContent"> 
      <table cellspacing="0" cellpadding="0" border="0" width="100%"> 
        <tbody> 
          <tr> 
            <td class="TableLeftCor"/>
            <td class="TableHeader"> Main </td> 
            <td class="TableRightCor"/>
          </tr> 
        </tbody>
      </table> 
      <table cellspacing="0" cellpadding="0" border="0" width="100%">
      <div id="copyright"> 
      </div> 
</form>

I have two-page classes:

LandingPage.java:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class LandingPage extends BasePage {

    public LandingPage(WebDriver driver) {
        super(driver);
        // TODO Auto-generated constructor stub
    }

    @FindBy(name = "username")
    private WebElement usernameTextbox;

    @FindBy(name = "password")
    private WebElement passwordTextbox;

    @FindBy(name = "submit")
    private WebElement loginButton;

    public MainPage login(String usrname, String passwd) throws InterruptedException{
        usernameTextbox.sendKeys(usrname);
        passwordTextbox.sendKeys(passwd);
        loginButton.click();
        loginButton.click();
        TimeUnit.SECONDS.sleep(3);
        System.out.println(driver.getCurrentUrl());
        return new MainPage(driver);
    }

}

MainPage.java

package pageClasses;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class MainPage extends BasePage {

    public MainPage(WebDriver driver) {
        super(driver);
        // TODO Auto-generated constructor stub
    }

    @FindBy(className = "TableHeader")
    private WebElement pageHeader;

    public void testMainPage(){
        System.out.println("inside main page");
        System.out.println(pageHeader.getText());
    }
}

marked as duplicate by Bharat Mane, IAmMilinPatel, FDM, NarendraC, ECiurleo Jul 12 '17 at 12:51

This question was marked as an exact duplicate of an existing question.

  • You have already posted the same question on yesterday & the interesting part is -You have accepted the answer. Do you have still any issue with locating element? If, YES Kindly update in the same question, instead of asking new one. – Bharat Mane Jul 12 '17 at 6:23
  • Make sure you don't have any iframe/frame, if yes then you need to switch to it first. – lauda Jul 12 '17 at 8:51
0

Don't use XPath if you don't have to.

Your selector could be as simple as:

By.ClassName("TableHeader")

or if you have multiple such tables in your page,

By.CssSelector(".PageContent .TableHeader")

If you must use XPath, then the simplest equivalent is:

By.XPath("//td[@class='TableHeader']")
  • I tried all these options, but they are not working. – Rose Jul 12 '17 at 6:31
  • Then you need to post the code because they should work - better than your too complex XPath. :) – FDM Jul 12 '17 at 6:33
  • posted my code, have a look – Rose Jul 12 '17 at 6:52
  • Are you sure the element is loaded when you reach that code? Try adding an explicit wait. – FDM Jul 12 '17 at 7:11
  • I am sure element is being loaded but as u said let me try this explicit wait. I have also given implicit wait , i can gv more implicit wait also – Rose Jul 12 '17 at 8:50
0

These are some alternatives:

css: #topNavigation tbody td + td

css: #topNavigation table tbody td + td

css: #topNavigation tbody td.TableHeader

css: #topNavigation table tbody td.TableHeader

xpath: //table[@id='topNavigation']//tbody//td[2]

xpath: //table[@id='topNavigation']//table/tbody//td[2]

xpath: //table[@id='topNavigation']//tbody//td[@class='TableHeader']

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