Selenium 2:在新选项卡中打开链接并关闭选项卡
我希望能够在 Selenium 2 的新选项卡中打开链接。此外,我想在完成与页面的交互后关闭该选项卡。如果我有 标记的
WebElement
,这怎么可能?
我正在使用 Selenium 2 的 Java API 和 Firefox 驱动程序,在 Firefox 4 上运行。
I want to be able to open a link in a new tab in Selenium 2. Also i want to close the tab when i am finished interacting with the page. How is this possible if I have a WebElement
of an <a>
tag?
I am using the Java API of Selenium 2 with the Firefox Driver, running on Firefox 4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我发现 selenium 2 在 Chrome 和 Firefox 上工作正常,IE 有安全检查问题:
The way I figure out for selenium 2, work fine for Chrome and firefox, IE has security check issue:
目前,Selenium WebDriver API 没有任何处理选项卡的方法。在我期望看到 Java 等语言绑定中的实现之前,该项目确实需要一组一致的、跨浏览器的方法来管理选项卡。在那之前,您的 JavaScript 解决方案可能是唯一的方法,请记住您的代码将负责管理该选项卡的生命周期。
At the moment, the Selenium WebDriver API doesn't have any way of handling tabs. The project would really need a consistent, cross-browser set of methods for managing tabs before I would expect to see an implementation in one of the language bindings like Java. Until then, your JavaScript solution may be the only way, and remember that your code would then be responsible for managing the lifetime of that tab.
为了充分利用 selenium,我们 sol-logics 将其与 java.awt.robot 类结合起来。您可以发送可以关闭浏览器窗口的密钥。尝试使用
并回复如果有效
to use selenium at its best we at sol-logics combine it with java.awt.robot class. you can send keys that can close a browser window. try using
and reply if it works
我花了一段时间(大约 2 周)才找到正确的命令顺序,但这是迄今为止我在 Win7/Chrome 设置中找到的在新选项卡中打开链接并切换到新选项卡的最简单方法自动地。
警告!确保始终执行 keyUp 操作。如果您无法执行 keyUp,您的系统将保持按下这些键,直到重新启动或发生 keyUp。
Windows 7/Chrome:
注意:我知道这是一个旧线程,我只是想在这里对解决方案进行编目,因为我找不到更优雅的解决方案,并且想为其他人节省一点时间(希望:)。
编辑:错别字
Took awhile (~2 weeks) for me to track down the right sequence of commands, but this is by far the easiest method I've found for a Win7/Chrome setup to open a link in a new tab AND switch to the new tab automatically.
WARNING! Make sure to always perform the keyUp actions. If you fail to perform keyUp your system will keep those keys pressed until a reboot or keyUp occurs.
Windows 7/Chrome:
Note: I know it's an old thread, I just wanted to catalog the solution here because I couldn't find a more elegant solution and wanted to save someone else a little time (hopefully :).
Edit: Typo
这是我使用 Python 的方法。
这个解决方案有点脏,但如果你想关闭选项卡,它就可以工作。
我模仿 Mac 快捷键 CMD + W 来关闭选项卡,如果您运行的是 Windows,则可能需要实现不同的组合键。
Here is how i did it using Python.
This solution is a bit dirty but it works if you want to close the tab.
Im mimicking the mac shortcut CMD + W to close a tab, if you are running windows you may have to implement a different key combination.
我使用的是 Robor 类。
这使得机器人快速按下并释放 CTRL + W 键来模拟用户交互。如果您只使用 keyPress 事件,这将关闭 WebDriver 的所有选项卡和窗口。
希望我对你有帮助。
What I use is the Robor class.
This makes the Robot rapidly press and release the CTRL + W keys to simulate a user interaction. If you only use keyPress event, this will close all the tabs and windows of the WebDriver.
Hope I helped you.