- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试运行以下代码。但每次我运行时都会打开一个新的 chrome 窗口。如何解决这个问题。此外,打开的新 chrome 实例未登录,因此我无法使用已安装的扩展程序。有办法解决这个问题吗?
任何帮助将不胜感激。
我已经提供了相同的屏幕截图:
代码试验:
package com.webdriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class WebElements {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
}
}
最佳答案
每次创建新的 ChromeDriver 实例时执行代码,都会生成新的 Chrome 浏览器 session 。此功能按照设计。
更好的方法是在 tearDown(){}
方法中调用 driver.quit()
来关闭并销毁 <每次运行测试执行后,em>WebDriver 和 Web 浏览器 实例都会正常运行。详细可以找discussion here .
虽然这可以解决多个 Chrome 浏览器窗口的问题,但您仍然无法在新 session 中登录。要登录,您需要使用特定的Chrome配置文件并存储cookie并在以后重复使用。您将在 this discussion 中找到有关如何创建和打开新的 Chrome 配置文件的详细讨论。 .
要使用您已安装的扩展,您需要打包/解压它们并使用 ChromeOptions
类。
Chrome 扩展程序可以打包或解包。打包扩展是带有 .crx 扩展名的单个文件。解压后的扩展是包含扩展的目录,其中包括 manifest.json
文件。
要打包未打包的扩展程序,请使用 chrome://extensions
中的“打包”按钮或使用 Chrome(Windows 示例):
chrome.exe --pack-extension=C:\path\to\unpacked\extension --pack-extension-key=C:\myext.pem
请参阅extensions docs了解其他对自动化更加友好的方法。要解压打包的扩展程序,只需解压缩文件(您可能需要将文件从 .crx
重命名为 .zip
以便 zip 实用程序识别它)。
打包(.crx 文件):
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
WebDriver driver = new ChromeDriver(options);
解压(目录):
ChromeOptions options = new ChromeOptions();
options.addArguments("load-extension=/path/to/extension");
WebDriver driver = new ChromeDriver(options);
关于java - 为什么 Selenium Webdriver 每次运行脚本都会打开新窗口以及如何修复它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54734087/
我正在尝试使用特定路线打开一个新窗口,但似乎路线页面未正确加载。将从FrmApprovalMain.js打开新的 Electron 窗口,并应使用FrmPOdet.js打开新窗口。但是,当它打开时,会
我对 Tkinter 比较陌生,我需要帮助。 当从父窗口单击按钮时,我创建了一个新窗口。新窗口是 def new_Window。但我似乎无法获取窗口中的信息,如下编码: from tkinter im
我正在尝试使用 Javascript 从空白 iframe 中创建一个重定向,该 iframe 将定向到新窗口或选项卡中的 URL。 更具体地说,我试图让一个 Facebook 标签指向我公司的网页,
在问这个问题之前,我确实搜索了很多。抱歉,如果这是一个简单的问题。 在我的 wxWidgets 应用程序中,我想从菜单选项中新建一个 wxHtmlWindow。 (我已经正确显示了我的主应用程序窗口)
我认为我不是很了解这个,因为我对 jquery 还很陌生。我有一个隐藏了 3 个食谱的页面。单击食谱 A 的链接时,它会以模式打开。我希望能够只打印食谱的内容。所以我打开一个新窗口(非模态)并尝试将食
我想在带有可滚动文本框的现有主 Windwoe 旁边创建一个新窗口。 我在主窗口中按下“打开新窗口”按钮,然后它应该会打开一个带有可滚动文本框的新窗口。 在 form2 中 在 WPF 中,您可以在主
我正在使用 Simple Icons 制作自定义 Pinterest Pin It 按钮,我正在使用 Pinterest Widget Builder 给我的链接。在默认的 Pin It 按钮上,它会
我的表单上出现一个确认对话框,单击“确定”后会将用户重定向到另一个页面。 但是,是否可以在新窗口中打开该页面(例如 target="_blank") 我正在使用的代码是: var answer=con
相关但处于休眠状态:Javascript to open URL within a new Tab和其他一些...... 我有一个客户坚持认为特定链接需要在新选项卡中打开,而不是在窗口中打开。它由表单
我遇到了 Android 问题 WebView , 我想用 target='_blank' 打开一个 URL在同一WebView ,就像所有其他的一样URLs正在开放。 另请注意,我重写了 WebVi
可以从 Controller 重定向到某个 url 并同时在新窗口中打开该 url 吗? 最佳答案 不是真的来自 Controller Action 。 重定向是带有 301/302 状态代码和 Ra
在 Electron vue 中,我想为每个新窗口创建新的 vue 实例。这工作正常,直到我想将路由器子组件加载到新窗口。例如我有条目 add: path.join(__dirname, '../sr
案例1: var printWindow = window.open("", "print_window"); if(printWindow) { printWindow.addEventLi
我需要使用 Javascript Window.Open 函数打开一个新的 (_blank) 窗口。 URL 编码的回车符/换行符 (%0A) 似乎不起作用。有谁知道解决这个问题?例如,我有以下 UR
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 1年前关闭。 Imp
当 flash 有键盘焦点时,CTRL+T(新标签)和 CTRL+N(新标签)窗口)被闪光拦截。 有没有办法将这些事件传递给浏览器以便它们工作(打开新选项卡,打开新浏览器)或者是否有用于这些操作的 j
我有一个 ASP.Net MVC 页面,上面有一个非常简单的表单:一个文本框和一个提交按钮。该表单使用标准的 mvc BeginForm() 语法: 当直接打开页面时,一切正常。 但是,我们有另一个
如果您对此问题感兴趣,请阅读下面的更新 #2 ;) 假设我将此代码放入我的扩展程序的 JS 中。 var reader = { onInputStreamReady : function(in
我是一名优秀的程序员,十分优秀!