-2

I have followig HTML code and want X path for the text "Analytics & Research"

<div id="LLCompositePageContainer" class="column-wrapper">
    <div id="compositePageTitleDiv">
        <h1 class="page-header">Analytics &amp; Research</h1>   
 </div> 

I am getting following xpath using chrome, but that didnt work.

//*[@id="compositePageTitleDiv"]

this is my code

WebElement header = driver.findElement(By.xpath("//div[@id='LLCompositePageContainer']/div[@id='compositePageTitleDiv']/h1[@class='page-header']"));        

String header2 = header.getText();

System.out.println(header2);

and following error I am getting

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == //div[@id='LLCompositePageContainer']/div[@id='compositePageTitleDiv']/h1[@class='page-header'] (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 10.34 seconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
Amol1981
  • 1
  • 5

5 Answers5

0

This is how it can be used :

WebElement element= driver.findElement(By.xpath("//*[@id='compositePageTitleDiv']"));

Or in case it is nested, can be accessed like this as well

WebElement element = driver.findElement(By.xpath("//html/body/div[3]/div[3]/"));

this is just a rough syntax.

Vishal_Kotecha
  • 481
  • 5
  • 19
  • Thanks, i know the syntax, but the x path that either i am getting from chrome or the one which i tried to create based on my research didnt worked. – Amol1981 Jul 21 '16 at 14:48
  • this is similar : http://stackoverflow.com/questions/2596264/trying-to-create-xpath-from-this-html-snippet?rq=1 – Vishal_Kotecha Jul 22 '16 at 05:17
0

No need to use Xpath here if you could simply locate the element using By.id(). Asuming are using Java, you should try as below :-

WebElement el = drive.findElement(By.id("compositePageTitleDiv"));
String text = el.getText();

Edited :- If element not found, may it is timing issues you need to implement WebDriverWait to wait for element until visible on the page as below :-

WebDriverWait wait = new WebDriverWait(webDriver, implicitWait);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("compositePageTitleDiv")));
String text = el.getText();

Note :- if your element is inside any frame, you need to switch that frame before finding element as :- driver.switchTo().frame("your frame name or id");

Hope it helps..:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • unable to find element by id, getting below error Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with id == compositePageTitleDiv (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 10.40 seconds – Amol1981 Jul 22 '16 at 04:16
  • @Amol1981 you need to implement `WebDriverWait` then, see updated answer.. – Saurabh Gaur Jul 22 '16 at 04:41
0

Please try to use the below xpath:

driver.findElement(By.xpath(".//div[@id='compositePageTitleDiv']/h1")).getText();

If the element is inside the iframe. Then use the below code:

// Switching to the frame
driver.switchTo().frame(<name>);

// Storing the value of the Analytics & Research
String text = driver.findElement(By.xpath(".//div[@id='compositePageTitleDiv']/h1")).getText();

// Switching back to original window
driver.switchTo().defaultContent();

Hope this helps.

k.s. Karthik
  • 731
  • 6
  • 16
0

You can also use

//div[@id='LLCompositePageContainer']/div[@id='compositePageTitleDiv']/
h1[contains(text(),'Analytics')]

This is the best way to reach to the specific web element, using contains minimize the chances of error.

Zohair
  • 268
  • 2
  • 7
-1

The correct xpath is

//div[@id='LLCompositePageContainer']
  /div[@id='compositePageTitleDiv']
  /h1[@class='page-header']

But you could have find your answer easily with some researchs on google...

Erwan
  • 51
  • 7
  • Thanks, i see how you have created the x path, i used the same but that didnt worked, do you know any tutorial using which i can learn to write x path? – Amol1981 Jul 21 '16 at 14:43