gpt4 book ai didi

Java selenium headless (headless)模式下载 PDF 问题

转载 作者:太空宇宙 更新时间:2023-11-04 10:05:07 25 4
gpt4 key购买 nike

下面的代码我尝试使用 selenium 从 chrome 浏览器下载 PDF。

public static void main(String args[]) throws InterruptedException, AWTException, IOException, DocumentException {

System.setProperty("webdriver.chrome.driver", "/home/sejalj/OtherProj/webDrivers/chromedriver_64");
ChromeOptions ChromeOptions = new ChromeOptions();
ChromeOptions.addArguments("--headless", "window-size=1024,768", "--no-sandbox");

WebDriver driver = new ChromeDriver(ChromeOptions);

String baseUrl = "http://url.com/";

driver.get(baseUrl);

driver.findElement(By.name("wl_user_name")).sendKeys("uname");
driver.findElement(By.name("wl_user_password")).sendKeys("password");
driver.findElement(By.cssSelector("input[value=Login]")).click();

String pdfUrl = "https://www.pdfurl.com/displayImageDocs.php?
f=MjAxODA3MjQxMDUwNzA4Ni5QREY=&p=aW1hZ2UuaW1hZ2ViYW5rLmJsdsaddhbmsxLjIwMTgwNy4yMDE4MDcyNA==&a=MTAwMjM0&POL_NUM=AAS06036999";";

// Opens pdf of specific URL
driver.get(pdfUrl);

Actions a = new Actions(driver);

// To press CTRL+S
a.keyUp(Keys.CONTROL).sendKeys("s").build().perform();

Robot robot = new java.awt.Robot();
robot.keyPress(KeyEvent.VK_BACK_SPACE);
robot.keyRelease(KeyEvent.VK_BACK_SPACE);
int keyInput[] = {KeyEvent.VK_A, KeyEvent.VK_A, KeyEvent.VK_A, KeyEvent.VK_A, KeyEvent.VK_UNDERSCORE,
KeyEvent.VK_P, KeyEvent.VK_O, KeyEvent.VK_L, KeyEvent.VK_I, KeyEvent.VK_C, KeyEvent.VK_Y
};

for (int i = 0; i < keyInput.length; i++) {
robot.keyPress(keyInput[i]);
robot.keyRelease(keyInput[i]);
}

// To press ENTER
robot.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(1000);
robot.keyRelease(KeyEvent.VK_ENTER);

System.out.println("Preparing Policies document...Please wait");
System.out.println("Document prepared..");
}

上面的代码在 chrome 没有 --headless 的情况下运行时工作正常模式。但在 --headless上面的模式代码不起作用。

Robot类不支持 headless (headless)模式。请指导。

Solution that has been mark as working can be found here: Download files in Java, Selenium using ChromeDriver and headless mode

最佳答案

尝试这个并删除 driver.get(pdfUrl);
之后的代码 这可能会帮助您使用带有对象的字符串的 HashMap,

<-- 语言:lang-java -->

HashMap<String, Object> prefs = new HashMap<>();        
prefs.put("plugins.always_open_pdf_externally", true);
ChromeOptions.setExperimentalOption("prefs", prefs);

关于Java selenium headless (headless)模式下载 PDF 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53039417/

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