0

I have a website that has such a piece of source:

<div class="wrapper group">
 <h2>
    <span>
    1.
    </span>
    Créez votre site.
 </h2>
</div>

I want to get this string

"<span>1.</span>Créez votre site."

to do some verification. How do I get such a string?

By the way, there is an existing piece of code in the java project to get the text:

public static final SeBlob STEP_ONE = new SeBlob("div.wrapper.group>h2");

 driver.findElement(WebsitesElements.STEP_ONE.getBy()).getText();

The result is "1. Créez votre site", which is not what I want.

Thomas Mueller
  • 48,905
  • 14
  • 116
  • 132
  • Two things: the way you describe it, you want the HTML of the entire `h2` tag, not the `span` tag as said in your title. Otherwise you'd be getting simply "1.", as opposed to the rest of it. Secondly, in what programming language is this? – Arran Aug 29 '13 at 14:10
  • Hi Arran, thank you very much! I have changed my description according to your suggestion. – thisisjingjing Aug 29 '13 at 14:23
  • Then in which case, this has been asked before: http://stackoverflow.com/questions/7263824/get-html-source-of-webelement-in-selenium-webdriver-python ...also the question is about Python, the accepted answer is Java. – Arran Aug 29 '13 at 14:31
  • Yes, according to your link, I can get the string using "driver.findElement(WebsitesElements.STEP_ONE.getBy()).getAttribute("innerHTML").trim();". Thank you so much! – thisisjingjing Aug 30 '13 at 01:13

2 Answers2

1

According to Arran's comments, I solved the problem with the following statement: "driver.findElement(WebsitesElements.STEP_ONE.getBy()).getAttribute("innerHTML")‌​.trim();".

0

The trick is to get the WebElement, then execute the Javascript command that retrieves the innerHTML

WebElement element = driver.findElement(WebsitesElements.STEP_ONE.getBy());

String contents = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;", element); 
j.r.clarkin
  • 516
  • 5
  • 6