gpt4 book ai didi

python - 如何在 WebDriver 测试中更改 Firefox 设置?

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

我在 Firefox 上用 Python 运行 WebDriver 测试。我已将我的 Firefox 配置为确保社交网站的所有链接都在当前选项卡中打开。我特意做了以下两处改动

browser.link.open_newwindow.restriction then,  change the value to 0 (zero)
browser.link.open_newwindow and change the value to 1 (one)

可以在https://support.mozilla.org/en-US/questions/970999中找到.

我的 WebDriver Firefox 安装包括

from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.common.action_chains import ActionChains

success = True
wd = WebDriver()
wd.implicitly_wait(60)

如何在开始测试代码之前将设置添加到上述设置中?

编辑

当我尝试更改 browser.link.open_newwindow 的值时出现以下错误

Exception in thread "main" java.lang.IllegalArgumentException: Preference     browser.link.open_newwindow may not be overridden: frozen value=2, requested value=1
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:120)
at org.openqa.selenium.firefox.Preferences.checkPreference(Preferences.java:223)
at org.openqa.selenium.firefox.Preferences.setPreference(Preferences.java:161)
at org.openqa.selenium.firefox.FirefoxProfile.setPreference(FirefoxProfile.java:230)
at tmp.main(tmp.java:21)

最佳答案

您可以使用这样的配置文件设置首选项。

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.link.open_newwindow.restriction", 0);
profile.setPreference("browser.link.open_newwindow", 1);
WebDriver webDriver = new FirefoxDriver(profile);

Python 代码可能看起来像这样,但我现在无法测试。

from selenium import webdriver
profile = webdriver.FirefoxProfile();
profile.set_preference("browser.link.open_newwindow.restriction", 0);
profile.set_preference("browser.link.open_newwindow", 1);
wd = webdriver.Firefox(profile);

来源:FirefoxDriver Tips & Tricks

关于python - 如何在 WebDriver 测试中更改 Firefox 设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24054404/

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