在硒的新标签页中打开链接

时间:2020-02-18 12:41:23

标签: selenium

我正在尝试在新标签页中打开链接。

Trial1

Actions action = new Actions(Driver).KeyDown(Keys.Control).KeyDown(Keys.Shift).Click(FindElement(xxx).KeyUp(Keys.Control).KeyUp(Keys.Shift);
         action.Build().Perform();

此代码在新标签页中打开链接,同时打开另一个空白窗口

试用2-右键单击链接,然后选择第一个选项“打开链接新标签”

      action.ContextClick(FindElement(xxx).SendKeys(Keys.ArrowDown).SendKeys(Keys.Enter).Build().Perform();

这将在新窗口而不是新标签页中打开链接

1 个答案:

答案 0 :(得分:0)

所以您需要做的是首先使用JS执行器打开一个新标签页:

((JavascriptExecutor)driver).executeScript("window.open()");

((JavascriptExecutor)driver).executeScript("window.open('the new link here', '_blank')");

如果您未使用secon方法,请继续执行以下操作:切换到新窗口并导航到链接

driver.switchTo().window(here the window handle);
driver.get("here is the link");
相关问题