Unable to retrieve text using .getText()?

1,249 views
Skip to first unread message

bert

unread,
Nov 15, 2013, 2:28:06 PM11/15/13
to seleniu...@googlegroups.com
HI,

I'm not able to retrieve the text from the following HTML:

<iframe width="800px" height="700px" frameborder="0" src="/cs/ContentServer?c=Article_C&pagename=NTMain%2FArticle_C%2FFeature%2FDetail&rendermode=preview&cid=1355791606640">
    <html>
        <head>
            <script src="//nexus.ensighten.com/website/Bootstrap.js" type="text/javascript">
                <style>
                <style type="text/css">
        </head>
        <body class="jsloaded" data-twttr-rendered="true">
            <div class="pageContent" role="main">
                <div class="width-8">
                    <div class="tile" role="article">
                        <h3>Test_Article_Fri Nov 15 17:19:42 GMT</h3>
                                <div class="articleTabs">
                                <div class="articleContent">
                            </div>
                        </div>
                    <div class="width-4">
                </div>
            <div id="tiptip_holder" style="max-width:200px;">
        </body>
    </html>
</iframe>

Here is my Webdriver code:

WebElement articleName = driver.findElement(By.cssSelector(".tile>h3"));
        articleName.getText();
        System.out.println(articleName);

What gets printed to console is simply the locator, rather than the string itself.

Any ideas?

Thanks

Shawn Knight

unread,
Nov 15, 2013, 4:15:49 PM11/15/13
to seleniu...@googlegroups.com
Does your code take into account the iframe?

bert

unread,
Nov 18, 2013, 5:52:38 AM11/18/13
to seleniu...@googlegroups.com
Hi,

Thank you - I can actually get the text from another area on the page without having to access the iframe.  So I have tried again but still the getText() is not working.  Here is the new HTML:

<span class="title-value-text">Test_New_Article_Mon Nov 18 09:53:13 GMT</span>

And my code:

WebElement articleName = driver.findElement(By.cssSelector(".title-value-text"));
articleName.getText();
System.out.println(articleName);

And what gets printed to the console is:

[[FirefoxDriver: firefox on XP (55a408f6-fccb-4ded-8f08-579fd550ba4d)] -> css selector: .title-value-text]

...instead of the text inside the Span tag "Test_New......." 

Is this a problem of getText() not working on Span tags?

Thanks

Alexra

unread,
Nov 18, 2013, 7:33:44 AM11/18/13
to seleniu...@googlegroups.com
I also had this problem and in order to get the text I used HtmlAgilityPack. I get the source from the driver, covert it to htmldocument and than I can get any element and it's text.

bert

unread,
Nov 18, 2013, 10:36:21 AM11/18/13
to seleniu...@googlegroups.com
Thanks.  I can get into the iFrame, but can't get the text.  I suspect it's the Span tags that are the problem.  Alexra - can you elaborate on how you used the HtmlAgilityPack?

Alexra

unread,
Nov 18, 2013, 10:48:01 AM11/18/13
to seleniu...@googlegroups.com
Alexra - can you elaborate on how you used the HtmlAgilityPack?

Sure,

1. You add to your namespaces "using HtmlAgilityPack;".
2. You go to driver and save the page source, which is string: string s = driver.PageSource;
3. You create a new html document: HtmlDocument Doc = new HtmlDocument();
4. You covert page source to htmldocument: Doc.LoadHtml(s);
5. Once you have an html document, you can get it's elements and inner/outer html either using lambda expressions or using the methods like GetElementbyId.
Good Luck!

Gustavo Godoy

unread,
Nov 18, 2013, 12:25:59 PM11/18/13
to seleniu...@googlegroups.com
Hi,

Have you tried this?

WebElement articleName = driver.findElement(By.
cssSelector(".tile>h3"));

        System.out.println(articleName.getText());


Thanks,

gustavo Godoy

http://lifegoeasy.blogspot.in/

unread,
Nov 18, 2013, 11:00:55 PM11/18/13
to seleniu...@googlegroups.com
You may try getAttribute method,

    System.out.println(driver.findElement(By.xpath(""")).getAttribute("value"));

bert

unread,
Nov 19, 2013, 3:09:21 AM11/19/13
to seleniu...@googlegroups.com
Gustavo - thank you! this works.

Gustavo Godoy

unread,
Nov 19, 2013, 4:47:54 AM11/19/13
to seleniu...@googlegroups.com
Great!!!

It's a Java problem, not in webdriver or how you access your html...

This line just acess the text, but do nothing....
        articleName.getText();

And this line you just print the object webelement....
        System.out.println(articleName);


Reply all
Reply to author
Forward
0 new messages