Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在“新建”选项卡中打开的Selenium驱动程序与社区共享知识描述的不一样_C#_Selenium_Tabs - Fatal编程技术网

C# 在“新建”选项卡中打开的Selenium驱动程序与社区共享知识描述的不一样

C# 在“新建”选项卡中打开的Selenium驱动程序与社区共享知识描述的不一样,c#,selenium,tabs,C#,Selenium,Tabs,我知道这是一个矛盾的话题。“正式”驱动程序不支持选项卡,但许多地方表示,新打开的选项卡将通过窗口句柄可用,我们可以使用句柄在选项卡之间切换。 (代码示例是C#但我希望问题中没有什么特殊的C#) 我正在尝试在新选项卡中打开链接。我成功了,浏览器显示新选项卡,但是驱动程序的windowhandles不包含新打开的选项卡,只包含一个原始窗口句柄。这似乎是合乎逻辑的,选项卡不是一个窗口,然而,在许多地方,它被描述为应该工作,并且驱动程序将选项卡视为窗口。我错过了什么 在新选项卡中打开: // Perfo

我知道这是一个矛盾的话题。“正式”驱动程序不支持选项卡,但许多地方表示,新打开的选项卡将通过窗口句柄可用,我们可以使用句柄在选项卡之间切换。 (代码示例是C#但我希望问题中没有什么特殊的C#)

我正在尝试在新选项卡中打开链接。我成功了,浏览器显示新选项卡,但是驱动程序的windowhandles不包含新打开的选项卡,只包含一个原始窗口句柄。这似乎是合乎逻辑的,选项卡不是一个窗口,然而,在许多地方,它被描述为应该工作,并且驱动程序将选项卡视为窗口。我错过了什么

在新选项卡中打开:

// Performing Ctrl + Click on my link:
new Actions(driver)
    .KeyDown(Keys.Control)
    .Click(myLink)
    .KeyUp(Keys.Control).Perform();

// driver.WindowHandles did **not** change, still contains one handle
// The newly opened tab can not be reached, because we can not even switch
// the driver to it.
// Performing context menu and "Open new Window" on my link
new Actions(driver)
    .ContextClick(myLink)
    .SendKeys("w")
    .Perform();
// driver.WindowHandles **changed**, contains 2 handles
// Switch to the newly opened window works:
driver.SwitchTo().Window(driver.WindowHandles.Last());
在新窗口中打开:

// Performing Ctrl + Click on my link:
new Actions(driver)
    .KeyDown(Keys.Control)
    .Click(myLink)
    .KeyUp(Keys.Control).Perform();

// driver.WindowHandles did **not** change, still contains one handle
// The newly opened tab can not be reached, because we can not even switch
// the driver to it.
// Performing context menu and "Open new Window" on my link
new Actions(driver)
    .ContextClick(myLink)
    .SendKeys("w")
    .Perform();
// driver.WindowHandles **changed**, contains 2 handles
// Switch to the newly opened window works:
driver.SwitchTo().Window(driver.WindowHandles.Last());
杂项资料:

  • 使用FirefoxV43.0.4
  • 使用官方Selenium C#bindings v2.48.2(nuget)
  • 操作系统Windows 7 64位
  • zillon中的一个地方,选项卡被描述为工作状态:(也可以查看所有答案和注释)

浏览器之间存在差异,例如在
Chrome
中,驱动程序可以识别两个窗口句柄。在
FireFox
中,我也只有一个窗口句柄,但重点放在新选项卡上

要在选项卡之间切换,可以使用
操作

action.KeyDown(Keys.Control).SendKeys("2").Perform(); //to switch to the second tab

在通过SendKeys切换到第二个选项卡后,驱动程序是否会“驱动”第二个选项卡?谢谢您的回答。我将测试它。然而,我认为在我的情况下,这种开关设备将不够健壮。我打算平行打开/关闭多达50个选项卡。迟早C#程序内部表示哪个选项卡是哪个索引将与浏览器同步…在IE 11的情况下,即使可视焦点发生变化,它实际上也不会切换到新选项卡。