更改 Chrome 设置(PDF 文档设置)使用 Java Selenium。

huangapple go评论83阅读模式
英文:

Change chrome settings (PDF Documents setting) using java selenium

问题

以下是翻译好的部分:

曾经我可以点击一个按钮,然后会下载一个PDF文件,然后我可以阅读它。
然而,现在PDF会在浏览器中打开,这使得从那里阅读变得困难,因为我会收到401错误。
我注意到我可以更改Chrome浏览器的设置,使得PDF文档在下载而不是在浏览器中打开。
在Chrome的设置中有一个开关(PDF文档 - “下载PDF文件,而不是自动在Chrome中打开它们”)。这可以在 chrome://settings/ 中找到。

我如何使用Selenium来进行更改呢?
我可以使用 ChromeOptions 吗?如果可以,应该怎么做?

提前感谢您的帮助。

英文:

I used to be able to click on a button and a PDF file downloaded and then I was able to read it.
However, now the PDF opens up in the browser which makes it difficult to read from there because I get a 401.
I noticed that I can change the chrome settings to that the PDF document downloads instead of opening in the browser.
There is a toggle in chrome settings (PDF Documents - "Download PDF Files instead of automatically opening them in Chrome"). This can be found in chrome://settings/.

How can I change it using selenium?
Can I use ChromeOptions? if yes, how?

Thanks in advance

答案1

得分: 1

是的,您可以使用ChromeOptions通过以下代码更改Chrome默认的PDF下载设置:

HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("plugins.always_open_pdf_externally", true);  // 下载PDF文件而不是在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);
英文:

Yes, you can use ChromeOptions to change the chrome default pdf download setting by using the code below:

 HashMap&lt;String, Object&gt; chromePrefs = new HashMap&lt;String, Object&gt;();  
 chromePrefs.put(&quot;plugins.always_open_pdf_externally&quot;, true);  // Download PDF files instead of automatically opening them in Chrome
 ChromeOptions options = new ChromeOptions();  
 options.setExperimentalOption(&quot;prefs&quot;, chromePrefs);  

 DesiredCapabilities cap = new DesiredCapabilities();
 cap.setCapability(ChromeOptions.CAPABILITY, options);  

 System.setProperty(&quot;webdriver.chrome.driver&quot;, &quot;driverpath\\chromedriver.exe&quot;);
 WebDriver driver = new ChromeDriver(options);

答案2

得分: 0

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中打开还是下载。

英文:

Selenium can indeed access the chrome settings menu and change options as you like. This is because the setting is simply a rendered HTML DOM, you can verify that by pressing F12 and going to Elements section. see below for an example case.

I used .elementToBeClickable() in my code below. This is perfect since you want to simply toggle the button. The .until() returns the element waited for so you can just append .click() on the end of the statement.

driverChrome.manage().window().maximize();
driverChrome.get(&quot;chrome://settings&quot;);
WebElement w = driverChrome.findElement(By.xpath(&quot;//iframe[@name=&#39;settings&#39;]&quot;));
driverChrome = driverChrome.switchTo().frame(w);

WebDriverWait wait = new WebDriverWait(driverChrome, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(&quot;//a[text()=&#39;Show advanced settings...&#39;]&quot;))).click();//****replace the text here

As you can see, I access the chrome settings and within that, I set an XPath to the text of the button (you may change this if you like). This the clicks/toggle the element and in your case will set whether to open in chrome or to download it.

huangapple
  • 本文由 发表于 2020年4月8日 03:22:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/61087817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定