0

Can anyone help me with the following issue I came across. I could not find any solutions for it so far.

I would like to take the text:'You are logged in as' from the following html via XPath,but as there are no HTML tags I could not get the text in any way, I have tried: xpath=//div[@class='div3']/img, but with no success.

Here is the html:

<body>
   <div id="container">
     <div id="header">
       <div class="div1">
         <div class="div2">
           <div class="div3" style="background-color: transparent;">
             <img style="position: relative; top: 3px;" alt="" src="view/image/lock.png">
             You are logged in as
             <span>admin</span>
          </div>
        </div>
        <div id="menu"></div>
        ...
ddavison
  • 28,221
  • 15
  • 85
  • 110
Mira
  • 67
  • 4
  • 7

4 Answers4

0
xpath="//div[@class='div3']"
elem = elem.find_element_by_xpath(xpath)
print elem.text

this is how you would write in python. though, this text would contain "admin" word also which you can simply remove.

pankaj udaas
  • 439
  • 2
  • 9
  • 14
  • Actually I tried with xpath="//div[@class='div3']",but when I make the assertion assertEquals(driver.findElement(By.xpath("xpath=//div[@class='div3']")).getText(), "You are logged in as"); i receive invalid selector error:Unable to locate element with xpath expression – Mira Feb 01 '14 at 20:28
  • 1
    thats because `xpath=//div[@class='div3']` is not valid xpath. @Mira, i think you are getting confused with the Selenium 1 api. in S1, you needed to prefix your selector with the kind. For example: `css=somecssselector` `id=someid`. In WebDriver, you do not do that. It's all controlled by the `By` class now. `By.id("someid")`, `By.xpath("//somexpath")` – ddavison Feb 01 '14 at 23:59
0

The element img in your html document is not a pair tag and therefore contains no text. Also, By.xpath method doesn't support the "xpath=" prefix.

The simplest way to get the text is to to query for the text of the parent div element.

The expression

driver.findElement(By.xpath("//div[@class='div3']")).getText()

returns "You are logged in as admin". Now it takes only a simple String expression to get the desired result. It is always a good idea to trim the text before making an assertion.

If you really need to get only the text between img and span elements (excluding the content of the span element), you might have to resort to JavaScript. Take a look at Reading text using selenium webdriver(xpath) .

Community
  • 1
  • 1
Tomas Pinos
  • 2,812
  • 14
  • 22
0

Firstly, get away from xpath.. It's slower, and it's less pretty than CSS.

So your issue is, is that you need to get the text You are logged in as. What you tried so far, is to fetch the text of the <img /> tag, but the <img /> tag does not have an innerHTML. What you need to fetch is the text of the immediate parent. You can do this by using this CSS selector:

Java:

driver.findElement(By.cssSelector("div#container div.div3")).getText();

So what this will return you, is the text You are now logged in as admin.

In your assert clause, write:

assertTrue(driver.findElement(By.cssSelector("div#container div.div3")).getText().contains("You are logged in as"));

Another thing i challenge you to do, is look at those <div> classes. Are div1 | div2 | div3 the actual classes they use? If not, could you post the actual html?

ddavison
  • 28,221
  • 15
  • 85
  • 110
  • I would argue with the fact that xpath is being slower. Latest tests I did a week ago actaully showed that xpath was quicker in FF25.0.1 when scraping tables. I have seen other people coming to same conclusions also, like this guy: http://elementalselenium.com/tips/32-xpath-vs-css . About the ugliness, it is a matter of taste - i find xpath more flexible as you can walk the markup both ways (from child to parent). The problem with xpaths is, that many people are not very good at writing them and firebug gives you really ugly and brittle ones. – Erki M. Feb 02 '14 at 09:08
  • Thank you all!@sircapsalot ,the css selector "div#container div.div3" solves my problem.I'll check further how can I use java script to take the text excluding span element. – Mira Feb 02 '14 at 20:44
0

So you're targeting content from this <div class="div3"...> element. You found that you could select it with the following XPath expression //div[@class="div3"].

Now, inside this element, you have several children (the XPath for direct children would be //div[@class="div3"]/node()):

  • a text node '\n ',
  • and img element (<img style="position...>),
  • another text node '\n You are logged in as\n ',
  • a span element (<span>admin</span>),
  • a final text node '\n '

When you want to select only children text nodes, you can use //div[@class="div3"]/text().

But I understand you want to extract the first non-all-whitespace text node, the one that has "You are logged in as". So you can use the normalize-space() function of XPath to test if the text content white whitespace removed is empty or not:

//div[@class="div3"]/text()[normalize-space() != ""]
paul trmbrth
  • 20,518
  • 4
  • 53
  • 66