1
votes

At first I go to the website www.google.de. I want to retrieve the word Deutschland from this HTML-Code:

<div class="logo-subtext">Deutschland</div>

I have to use xpath, because id in this div class is not available. Here you will see the HTML-source code --> view-source:https://www.google.de/?gws_rd=ssl

  1. How to load the HTML of the website into a robot variable?
  2. How to find the div-Element and get the text out of it?

Code-Snippet of my Robot-File until now:

*** Settings ***
Library           Selenium2Library

*** Test Cases ***
xpath-test
    Open Browser    https://www.google.de/    browser=firefox
    ${elem} =    Get WebElement    xpath=//div[text()="Deutschland"]
1
Please show a complete sample of your HTML input, see stackoverflow.com/help/mcve. Then, 2) can be answered. - Mathias Müller
try xpath=//div[text()="Deutschland"] and why do you want it in a variable ? Just use Wait Until Page Contains Element or Page Should Contain Element for that div. Assuming you are using Selenium2Library - Newcomer
@kame, As Newcomer suggested, you should be able to proceed with that xpath as long the text is uniquely located. - Rao
Hi, I made an edit in the code. The problem is, that I don't know the word. Maybe something else then Deutschland. - I don't need it as a variable. But how can I show log the word which I retrieved from the HTML in the end? - kame
//div[@class = 'logo-subtext'] returns the correct element, but someone else has to explain how to do this in the robot framework. - Mathias Müller

1 Answers

4
votes

Selenium2Library has a keyword called Get Text which will return the text inside an element. All you need to do is provide a locator for the element.

If the web page has only a single div with the class logo-subtext you can use an xpath such as xpath=//div[@class='logo-subtext'].

Here's a complete working example, using pipe-separated format for clarity:

*** Settings ***
| Library | Selenium2Library
| Suite Teardown | Close all browsers

*** Test Cases ***
| xpath-test
| | Open Browser  | https://www.google.de/ | browser=firefox
| | ${logo text}= | get text | xpath=//div[@class='logo-subtext']
| | log | the logo text is ${logo text}
| | should be equal as strings | ${logo text} | Deutschland