Selenium 2:在新选项卡中打开链接并关闭选项卡

发布于 2024-11-07 10:53:14 字数 193 浏览 0 评论 0原文

我希望能够在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

俯瞰星空 2024-11-14 10:53:14

我发现 selenium 2 在 Chrome 和 Firefox 上工作正常,IE 有安全检查问题:

Set<String> winSet = webDriver.getWindowHandles();
List<String> winList = new ArrayList<String>(winSet);
String newTab = winList.get(winList.size() - 1);
webDriver.close(); // close the original tab
webDriver.switchTo().window(newTab); // switch to new tab

The way I figure out for selenium 2, work fine for Chrome and firefox, IE has security check issue:

Set<String> winSet = webDriver.getWindowHandles();
List<String> winList = new ArrayList<String>(winSet);
String newTab = winList.get(winList.size() - 1);
webDriver.close(); // close the original tab
webDriver.switchTo().window(newTab); // switch to new tab
妄想挽回 2024-11-14 10:53:14

目前,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.

番薯 2024-11-14 10:53:14

为了充分利用 selenium,我们 sol-logics 将其与 java.awt.robot 类结合起来。您可以发送可以关闭浏览器窗口的密钥。尝试使用

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_W);

并回复如果有效

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

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_W);

and reply if it works

给我一枪 2024-11-14 10:53:14

我花了一段时间(大约 2 周)才找到正确的命令顺序,但这是迄今为止我在 Win7/Chrome 设置中找到的在新选项卡中打开链接并切换到新选项卡的最简单方法自动地。

警告!确保始终执行 keyUp 操作。如果您无法执行 keyUp,您的系统将保持按下这些键,直到重新启动或发生 keyUp。

Windows 7/Chrome:

WebElement elem = driver.findElement(By.linkText("MyLinkText"));

// Chrome key combos:
//   SHIFT + CTRL + click = Open in new tab (and switch to new tab)
//   SHIFT + CTRL + RETURN = Open in new tab (in background)
Actions act = new Actions(driver);
act.keyDown(Keys.LEFT_CONTROL).keyDown(Keys.LEFT_SHIFT).perform();

// Wrap in a try/catch during implementation to ensure you perform keyUp(s).
elem.click();

act.keyUp(Keys.LEFT_CONTROL).keyDown(Keys.LEFT_SHIFT).perform();

注意:我知道这是一个旧线程,我只是想在这里对解决方案进行编目,因为我找不到更优雅的解决方案,并且想为其他人节省一点时间(希望:)。

编辑:错别字

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:

WebElement elem = driver.findElement(By.linkText("MyLinkText"));

// Chrome key combos:
//   SHIFT + CTRL + click = Open in new tab (and switch to new tab)
//   SHIFT + CTRL + RETURN = Open in new tab (in background)
Actions act = new Actions(driver);
act.keyDown(Keys.LEFT_CONTROL).keyDown(Keys.LEFT_SHIFT).perform();

// Wrap in a try/catch during implementation to ensure you perform keyUp(s).
elem.click();

act.keyUp(Keys.LEFT_CONTROL).keyDown(Keys.LEFT_SHIFT).perform();

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

哭了丶谁疼 2024-11-14 10:53:14

这是我使用 Python 的方法。

这个解决方案有点脏,但如果你想关闭选项卡,它就可以工作。

我模仿 Mac 快捷键 CMD + W 来关闭选项卡,如果您运行的是 Windows,则可能需要实现不同的组合键。

import from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.amazon.com/gp/search/ref=sr_in_-2_p_lbr_brands_browse-_2895?rh=n%3A172282%2Cn%3A!493964%2Cn%3A502394%2Cp_lbr_brands_browse-bin%3ALytro")
action_chains = ActionChains(driver)
action_chains.key_down(Keys.COMMAND + "w")
action_chains.perform()
action_chains.key_up(Keys.COMMAND + "w")
driver.implicitly_wait(5)

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.

import from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.amazon.com/gp/search/ref=sr_in_-2_p_lbr_brands_browse-_2895?rh=n%3A172282%2Cn%3A!493964%2Cn%3A502394%2Cp_lbr_brands_browse-bin%3ALytro")
action_chains = ActionChains(driver)
action_chains.key_down(Keys.COMMAND + "w")
action_chains.perform()
action_chains.key_up(Keys.COMMAND + "w")
driver.implicitly_wait(5)
美人如玉 2024-11-14 10:53:14

我使用的是 Robor 类。

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_W);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_W);

这使得机器人快速按下并释放 CTRL + W 键来模拟用户交互。如果您只使用 keyPress 事件,这将关闭 WebDriver 的所有选项卡和窗口。

希望我对你有帮助。

What I use is the Robor class.

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_W);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_W);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文