我曾经可以点击一个按钮,下载一个PDF文件,然后我就可以阅读它了。但是,现在PDF在浏览器中打开,这使得我很难从那里读取,因为我得到的是401。我注意到,我可以将chrome设置更改为PDF文档下载,而不是在浏览器中打开。chrome设置中有一个切换(PDF文档-“下载PDF文件而不是在Chrome中自动打开它们”)。这可以在chrome://settings/中找到。
如何使用selenium进行更改?我可以使用ChromeOptions吗?如果是,是如何实现的?
提前感谢
发布于 2020-04-09 00:18:12
可以,您可以使用以下代码使用ChromeOptions更改chrome默认pdf下载设置:
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("plugins.always_open_pdf_externally", true); // Download PDF files instead of automatically opening them in Chrome
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(ChromeOptions.CAPABILITY, options);
System.setProperty("webdriver.chrome.driver", "driverpath\\chromedriver.exe");
WebDriver driver = new ChromeDriver(options);发布于 2020-04-08 10:36:09
Selenium确实可以访问chrome设置菜单并随心所欲地更改选项。这是因为该设置只是一个呈现的HTML DOM,您可以通过按F12并转到Elements部分来验证这一点。请参见下面的示例案例。
我在下面的代码中使用了.elementToBeClickable()。这是完美的,因为你想简单地切换按钮。.until()返回等待的元素,因此只需将.click()附加到语句的末尾即可。
driverChrome.manage().window().maximize();
driverChrome.get("chrome://settings");
WebElement w = driverChrome.findElement(By.xpath("//iframe[@name='settings']"));
driverChrome = driverChrome.switchTo().frame(w);
WebDriverWait wait = new WebDriverWait(driverChrome, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='Show advanced settings...']"))).click();//****replace the text here正如您所看到的,我访问了chrome设置,并在其中设置了按钮文本的XPath (如果您愿意,可以对其进行更改)。这是点击/切换元素,在你的例子中将设置是在chrome中打开还是下载它。
https://stackoverflow.com/questions/61087817
复制相似问题