5

I want to use Selenium WebDriver to compile html String, the only examples i found are for opening files like :

WebDriver driver = new FirefoxDriver();
driver.get("file:///C:/Users/jgong/Desktop/a.html");

I need:

WebDriver driver = new FirefoxDriver();
driver.get(htmlString);

and then use the getPageSource() method, is it possible?

Thanks.

3
  • What do you mean by compile html String? this is very unclear. Commented May 29, 2017 at 9:12
  • Compile the js + html like the browser does. Commented May 29, 2017 at 9:29
  • 1
    @ItsikMauyhas Selenium is a free (open source) automated testing suite for web applications across different browsers and platforms. Selenium cannot compile HTML files. When you mention driver.get("file:///C:/Users/jgong/Desktop/a.html"); Selenium will open the a.html page through the browser of your choice. Thanks Commented May 29, 2017 at 10:09

1 Answer 1

11

You can use the data URI scheme to load some HTML with Selenium:

WebDriver driver = new FirefoxDriver();
driver.get("data:text/html;charset=utf-8," + htmlString);
Sign up to request clarification or add additional context in comments.

2 Comments

It didn't work for me. All the html code was put in the address bar and driver.find_element was not returning anything.
It has serious limitations. It is not able to parse inline styles. If you used any inline style (that contains color), it will break and stop parsing the remainder of the page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.