gpt4 book ai didi

java - 在java中使用Phantomjs在selenium中另存为pdf

转载 作者:行者123 更新时间:2023-11-30 07:25:55 25 4
gpt4 key购买 nike

我想在java中使用selenium执行一些任务

//java code
WebDriver driver = new PhantomJSDriver();
driver.get("http://www.example.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Guru99");
element.submit();

提交表单后,我想将结果页面保存为 pdf。我怎样才能做到这一点。我对其他编程语言一无所知,所以请帮助使用java代码。

最佳答案

这是您可以采取的措施来实现您的结果:-

提交文本后导航到结果页面后,您需要使用以下方式获取页面的:-

String htmlFileContent = driver.getPageSource();

然后您可以创建一个包含以下 html 内容的文件,如下所示:-

File file = new File("index.html");
file.createNewFile();
PrintWriter pw = new PrintWriter("index.html", "UTF-8");
pw.print(htmlFileContent);

将内容写入 HTML 文件后。您可以使用 iText Library 或任何其他 pdf 转换器库将 html 转换为 pdf

希望对你有帮助!!

关于java - 在java中使用Phantomjs在selenium中另存为pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36814898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com