R.Stevens
R.Stevens

Reputation: 115

Xpath Selenium trouble

Can anyone help me? i tried using Firepath for a correct Xpath however the code it gives me is incorrect in my eyes. First line in the examples, is the provided one.

.//*[@id='content']/div/div/div[1]/h2/span

<div id="content" class="article">
  <h1>News</h1>
  <div>
    <div>
      <div class="summary">
        <h2>
<span>9</span>
// this should be the correct xpath i think 
_driver.findElement(By.xpath("//*div[@id='content']/div/span.getText()"));

Here i want check if the text in between is greater or equal to 1

and the other is:

.//*[@id='content']/div/div/div[3]

<div id="content" class="article">
  <h1>News</h1>
  <div>
    <div>
      <div class="summary">
        <div class="form fancy">
          <div class="common results">

Here i want to check if the div class common results has been made, 1 item equals 1 common results

Upvotes: 1

Views: 121

Answers (2)

Juhi Saxena
Juhi Saxena

Reputation: 1217

For retrieving span text you can use this

String spanText=driver.findElement(By.xpath("//div[@id='content']/div/div/div/h2/span")).getText();
System.out.println(spanText);

From the second question I am not so much clear.You can get class name like this, Please explain me if its not your solution

String className=driver.findElement(By.xpath("//*[@id='content']/div/div/div/div/div")).getAttribute("class");
System.out.println(className);

Upvotes: 1

aberna
aberna

Reputation: 5814

I would suggest you making usage of:

//div[@id='content']/div/div/div/h2/span/text()

Note: the html code you shared was not well formed. I would suggest you to test in advance the code and the xpath with http://www.xpathtester.com/xpath (to fix the code) and http://codebeautify.org/Xpath-Tester (to test your xpath)

Upvotes: 1

Related Questions