S
S
stas09092021-10-18 15:13:35
Java
stas0909, 2021-10-18 15:13:35

How to save http page in selenium java pc?

Good afternoon. I'm trying to save an HTTP page to my computer using WebDriver driver = new FirefoxDriver(); using the hot keys CTRL + S; here is the code i use

System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.navigate().to(" https://www.google.com ");
TimeUnit.SECONDS.sleep(5);
Actions a = new Actions(driver);

a.keyDown(Keys.CONTROL).perform();
a.sendKeys("S").perform();
a.keyUp(Keys.CONTROL).perform();
a.sendKeys(Keys.ENTER).perform();

driver.close();

but nothing happens and the build completes successfully. what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-10-18
Hasanly @azerphoenix

Good afternoon.

How to save http page in selenium java pc?

It seems to me that you have gone the wrong way and, moreover, a perverted way.
First, why do you need to launch a browser, use selenium, etc. to save the page? This is costly in terms of time and memory.
You can make a get request to the server and get the byte array of the page itself, save it.
www.java2s.com/Tutorial/Java/0320__Network/SaveWeb...
https://stackoverflow.com/questions/17440236/getti...
Or at worst, use jsoup.
Here, a couple of lines:
public void downloadPage() throws Exception {
        final Response response = Jsoup.connect("http://www.example.net").execute();
        final Document doc = response.parse();

        final File f = new File("filename.html");
        FileUtils.writeStringToFile(f, doc.outerHtml(), StandardCharsets.UTF_8);
    }

Source - https://stackoverflow.com/questions/24696766/how-t...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question