Unable to extract the text using xpath on WebDriver version 2.42.2

723 views
Skip to first unread message

Praveen Shivashankar

unread,
Sep 10, 2014, 6:54:10 AM9/10/14
to seleniu...@googlegroups.com
Hello All,

For quite some time now, I have been trying to capture the text from a web page but have found absolutely no luck so far.  The HTML of the content is given below. As highlighted, I am trying to extract the "Identification Requests" content from the span class "panel-text.panel-title.ng-binding".  I have tried so many options that I have literally lost count of them but none of it seems to work. I keep running into element not found error.  Now, the page contains a whole lot of graphs and each graphical content has a particular title. So there are many elements like this in the webpage. Even trying to capture them into a List <WebElement> results in '0' elements!!!!! 

The most mind-boggling thing is.. the xpath that web Driver refuses to recognize works absolutely fine with Selenium RC!! I cannot figure out why an xPath that works for RC does not work for WebDriver!!!!!!!!! 

<div style="position:relative">
<div class="panel-container" ng-style="{'min-height':row.height}">
<kibana-panel class="" type="panel.type">
<div class="ng-scope" style="min-height:250px" ng-init="init()" ng-controller="histogram">
<div class="panel-header">
<div class="row-fluid">
<div class="row-fluid panel-extra">
<div class="panel-extra-container">
<span class="extra row-button" ng-show="panel.editable != false && panel.removable != false">
<span class="extra row-button" ng-hide="panel.draggable == false">
<span class="row-button extra" ng-show="panel.editable != false">
<span class="row-button extra ng-scope" ng-show="task.show" ng-repeat="task in panelMeta.modals">
<span class="row-button extra ng-scope" ng-show="task.show" ng-repeat="task in panelMeta.modals">
<span class="row-button extra ng-scope" ng-show="task.show" ng-repeat="task in panelMeta.modals">
<span class="row-button extra" ng-show="panelMeta.loading == true" style="display: none;">
<span class="panel-text panel-title ng-binding">Identification requests</span>
</div>
</div>
</div>
<div class="panel-content">

Below are some of the options that I have tried:

List<WebElement> someStrings = driver.findElements(By.xpath("//span[class='panel-text.panel-title.ng-binding']"));

List<WebElement> elements = driver.findElements(By.xpath("//div[*/class()='panel-header']/descendant::span[@class='panel-text.panel-title.ng-binding']"));

String spanCssSelector ="span.panel-text.panel-title.ng-binding";
List <WebElement> spanItem=driver.findElements(By.cssSelector(spanCssSelector));*/
String textObtained= spanItem.getText().trim();
assertEquals(textObtained,"Identification requests");  

 try {
       assertEquals("Identification requests", driver.findElement(By.xpath("html/body/div[2]/div[2]/div/div/div[1]/div/div[2]/div[2]/div/div/kibana-panel/div/div[1]/div[2]/div/span[8]")).getText());
} catch (Error e) {
     verificationErrors.append(e.toString());


Can someone please help me in resolving this?!! I am really at my wit's end! :( :( :(

Thanks,


P.S. I have also attached the snapshot of the page. 
Firepath.jpg
Page1.jpg

Mr.Krishna Reddy Gtpv

unread,
Sep 10, 2014, 7:25:19 AM9/10/14
to seleniu...@googlegroups.com

try with following xpaths

1.   //span[text()='Identification requests']
2.   //span[starts-with(@class,'panel-text panel-title')]

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/8af5786c-d79c-4316-bd9a-6f483de97302%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
-- 
Thanks & Regards,
Krishna Reddy Gtpv





Krishnan Mahadevan

unread,
Sep 10, 2014, 7:26:49 AM9/10/14
to Selenium Users
Try replacing 

String textObtained= spanItem.getText().trim();

with 

String textObtained= (String) driver.executeScript("return arguments[0].innerHTML;", spanItem);

and see if that helps.

I am assuming that your xPath is returning your span element correctly but you aren't getting any text because the span element doesnt have the value attribute which is what WebDriver is trying to query.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Praveen Shivashankar

unread,
Sep 10, 2014, 9:23:12 AM9/10/14
to seleniu...@googlegroups.com
@Krishnan,

Thanks for the response. I replaced the line as suggested, but it is giving an error that "The method executeScript(String, WebElement) is undefined for the type WebDriver"

Should I be importing some other package to address this?

Mr.Krishna Reddy Gtpv

unread,
Sep 10, 2014, 9:37:08 AM9/10/14
to seleniu...@googlegroups.com
No need to import anything, just let share with us html page and code which you have written, I have tried with xpath which i have given , that is working fine.


For more options, visit https://groups.google.com/d/optout.



--
--
Thanks & Regards
Krishna Reddy Gtpv
Automation Test Engineer
Web Site :www.gtpvkr.in
Email Id   :gtp...@gmail.com
Ph No      :+91 9494585126





Krishnan Mahadevan

unread,
Sep 10, 2014, 9:38:38 AM9/10/14
to Selenium Users
Praveen,

This normally occurs when you are working with the WebDriver as a reference (WebDriver interface doesnt have the signatures for this method and they are present in the JavascriptExecutor interface).


I am guessing you have something like this in your code 

WebDriver driver = new FirefoxDriver();

So you can do something like the below before using that statement.

JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
String textObtained= (String) jsExecutor.executeScript("return arguments[0].innerHTML;", spanItem);



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Praveen Shivashankar

unread,
Sep 10, 2014, 10:48:30 AM9/10/14
to seleniu...@googlegroups.com
Hi Krish,

Thanks again for your inputs. I have provided below my java code. The problem is that there are no elements at all for the script to work upon. The List<WebElement> is returning with 0 objects in it. The HTML code of the page is in the original question posted above. 

  @Test
 public void testIdentificationDetails() throws Exception{
 driver.get(baseUrl + "/#/home");
 driver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/ul/li[2]/a")).click();
 
   driver.findElement(By.id("username")).clear();
   driver.findElement(By.id("username")).sendKeys("asdf");
   driver.findElement(By.id("password")).clear();
   driver.findElement(By.id("password")).sendKeys("asadfadf");
   
   driver.findElement(By.cssSelector("button.btn.btn-default")).click();   
   Thread.sleep(10000);
   driver.findElement(By.xpath("//div[1]/div/div/div[2]/ul/li[2]/a")).click();
   Thread.sleep(10000);
   driver.findElement(By.xpath("//td/button")).click();
   Thread.sleep(20000);    
   
   
   String spanCssSelector ="span.panel-text.panel-title.ng-binding";
   List <WebElement> spanItem=driver.findElements(By.cssSelector(spanCssSelector));
   JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
   String textObtained= (String) jsExecutor.executeScript("return arguments[0].innerHTML;", spanItem);
   //assertEquals(textObtained,"Identification requests");   
   
   List<WebElement> someStrings = driver.findElements(By.xpath("//span[starts-with(@class='panel-text.panel-title')]"));
// List<WebElement> someStrings = driver.findElements(By.xpath("//span[starts-with(@class='panel-text.panel-title.ng-binding')]"));
 //String kk = return driver.findElements(By.id("pieLabel0")).
 List<WebElement> elements = driver.findElements(By.xpath("//div[*/class()='panel-header']/descendant::span[@class='panel-text.panel-title.ng-binding']"));
 
   //String someElement = driver.findElement(By.cssSelector("#pieLabel0>div")).getText();
   //driver.findElement(By.xpath("//*[(@id='03Q-legend')]/span[2]/span/i")).getText();
   //System.out.println(someElement);
       for (WebElement el:  elements)
       {
            //el.click();
        if (el.isDisplayed()){
        new Actions(driver).moveToElement(el).perform();
         
        if (el.getAttribute("text").isEmpty()) {
        System.out.println("There is no text here.");
        }
        else{
        String chartValue = el.getAttribute("text");
            System.out.println(chartValue);
        }
        }
       } 
   
   /*try {
     assertEquals("Identification requests", driver.findElement(By.xpath("//span[8]")).getText());
   } catch (Error e) {
     verificationErrors.append(e.toString());
   }*/
     
   try {
     assertEquals("Identification requests", driver.findElement(By.xpath("html/body/div[2]/div[2]/div/div/div[1]/div/div[2]/div[2]/div/div/kibana-panel/div/div[1]/div[2]/div/span[8]")).getText());
   } catch (Error e) {
     verificationErrors.append(e.toString());
   }
   try {
     assertEquals("Domain", driver.findElement(By.xpath("//div[2]/div/div[2]/div/div/div/kibana-panel/div/div/div[2]/div/span[8]")).getText());
   } catch (Error e) {
     verificationErrors.append(e.toString());
   }
   try {
     assertEquals("Authentication Policies", driver.findElement(By.xpath("//div[2]/div/div[2]/div[2]/div/div/kibana-panel/div/div/div[2]/div/span[8]")).getText());
   } catch (Error e) {
     verificationErrors.append(e.toString());
   }
   try {
     assertEquals("Candidite List Gender breakdown", driver.findElement(By.xpath("//div[3]/div/div[2]/div[2]/div/div/kibana-panel/div/div/div[2]/div/span[8]")).getText());
   } catch (Error e) {
     verificationErrors.append(e.toString());
   }
   try {
     assertEquals("candidate list year of birth breakdown", driver.findElement(By.xpath("//div[3]/div/div/kibana-panel/div/div/div[2]/div/span[8]")).getText());
   } catch (Error e) {
     verificationErrors.append(e.toString());
   }
   try {
     assertEquals("CANDIDATE LIST SIZE", driver.findElement(By.xpath("//div[4]/div/div[2]/div/div/div/kibana-panel/div/div/div[2]/div/span[8]")).getText());
   } catch (Error e) {
     verificationErrors.append(e.toString());
   }
   driver.findElement(By.linkText("Sign Out")).click();  
 }

The error I get is as follows:
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"span.panel-text.panel-title.ng-binding"}
Command duration or timeout: 60.10 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:30'
System info: host: 'PShivashankar', ip: '192.168.12.28', os.name: 'Windows 8.1', os.arch: 'x86', os.version: '6.3', java.version: '1.7.0_67'
Session ID: a70f46ee-890c-4dd6-93c9-4101ef39941a
Driver info: org.openqa.selenium.firefox.FirefoxDriver
  


Also, do you have any thoughts on why the xpath that works for RC does not work for the WebDriver?! I mean its rather counter intuitive!! 




Krishnan Mahadevan

unread,
Sep 10, 2014, 11:01:27 AM9/10/14
to Selenium Users
Praveen,

You might want to open up your web page in Chrome, open up Developer tools, and from the console validate your xpath by typing it as below :

Lets say your xPath is "//div"

Then you would type : $x("//div")

This would basically list out all elements that satisfy your xPath. Its an easy way to figure out if your xPath is the correct one or not.



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

Xiang Dong

unread,
Sep 10, 2014, 9:16:02 PM9/10/14
to seleniu...@googlegroups.com
your xpath maybe wrong, try below:

WebElement element =  driver.findElement(By.xpath("//span[@class='panel-text panel-title ng-binding']"));

add @ before class and change the dot to blank

--david


Date: Wed, 10 Sep 2014 03:54:10 -0700
From: praveen.sh...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Unable to extract the text using xpath on WebDriver version 2.42.2

Hello All,

For quite some time now, I have been trying to capture the text from a web page but have found absolutely no luck so far.  The HTML of the content is given below. As highlighted, I am trying to extract the "Identification Requests" content from the span class "panel-text.panel-title.ng-binding".  I have tried so many options that I have literally lost count of them but none of it seems to work. I keep running into element not found error.  Now, the page contains a whole lot of graphs and each graphical content has a particular title. So there are many elements like this in the webpage. Even trying to capture them into a List <WebElement> results in '0' elements!!!!! 

The most mind-boggling thing is.. the xpath that web Driver refuses to recognize works absolutely fine with Selenium RC!! I cannot figure out why an xPath that works for RC does not work for WebDriver!!!!!!!!! 

<div style="position:relative">
<div class="panel-container" ng-style="{'min-height':row.height}">
<kibana-panel type="panel.type">
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages