FirefoxDriver使用已打开的浏览器
要使用已打开的Firefox浏览器,您需要使用Firefox的远程调试功能。以下是使用FirefoxDriver连接到已经打开的Firefox浏览器的步骤:
- 打开一个新的Firefox浏览器窗口,并导航到
about:config页面。 - 在搜索框中搜索
devtools,找到devtools.debugger.remote-enabled选项,并将其设置为true。 - 在搜索框中搜索
devtools.chrome.enabled,找到devtools.chrome.enabled选项,并将其设置为true。 - 在浏览器地址栏中输入
about:debugging并按下回车键。 - 在
about:debugging页面,找到"远程调试"部分,并单击"启用"按钮。 - 在启用远程调试后,您将看到一个类似
"Remote debugging enabled on port 6000"的消息。请记下此端口号,因为您将在代码中使用它。 - 在您的Java代码中,您需要使用
FirefoxOptions和FirefoxDriver来连接到已经打开的Firefox浏览器。以下是示例代码:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
public class Main {
public static void main(String[] args) {
FirefoxOptions options = new FirefoxOptions();
options.setCapability("devtools.debugger.remote-enabled", true);
options.setCapability("devtools.debugger.remote-port", 6000); // 使用上面记下的端口号
WebDriver driver = new FirefoxDriver(options);
// 在已打开的浏览器上执行操作
driver.get("https://www.example.com");
// ...
driver.quit();
}
}
请确保将6000更改为在步骤6中记下的实际端口号。
这样,您就可以使用已打开的Firefox浏览器进行自动化测试了。请注意,您需要保持已打开的Firefox浏览器在远程调试模式下,否则代码将无法连接到它
原文地址: https://www.cveoy.top/t/topic/hLwD 著作权归作者所有。请勿转载和采集!