如何在使用VB.NET和C#中的ChromeDriver运行Selenium测试时将Chrome浏览器带到焦点?

huangapple go评论83阅读模式
英文:

How can I bring Chrome browser to focus when running a Selenium test using ChromeDriver in VB.NET and C#?

问题

在使用 ChromeDriver 执行 Selenium 测试时,我想将测试浏览器置于前台,以便看到操作过程。目前我能做到的方式是手动点击 Chrome 的实例,否则测试会运行,但我看不到操作过程。我希望能自动实现焦点切换。

我尝试过以下方法,但没有成功:

  String currentWindow = driver.getWindowHandle();
  driver.switchTo().window(currentWindow);
英文:

When executing a Selenium test with ChromeDriver, I would like to bring the test browser into focus to see the actions taking place. The way I am able to do that now is by manually clicking the instance of Chrome -- otherwise, the tests do run, I just don't see them taking place. I'd love for the focus part to happen automatically.

I have tried this with no luck:

  String currentWindow = driver.getWindowHandle();
  driver.switchTo().window(currentWindow);

答案1

得分: 0

我找到了解决方法:
在C#和VB.NET中:

driver.SwitchTo().Window(driver.WindowHandles(0))

注意:数字0是最后打开的选项卡。因此,如果您在浏览器中只打开了一个选项卡,此代码将正常工作。但是,如果您打开了多个选项卡,您应该将此数字设置为您希望显示的选项卡。例如,如果您想要打开倒数第二个选项卡之前打开的选项卡,您应该将其设置为1。

英文:

I found the solution:
In C# and VB.NET :

driver.SwitchTo().Window(driver.WindowHandles(0))

NOTE: The number 0 is the last opened tab. So if you have only one tab opens in browser, this code works fine. But if you have more than one tabs open, you should set this number as which tab you want to come up. For example if you want to open the tab which opened before last tab, you should set it to 1.

huangapple
  • 本文由 发表于 2023年6月11日 22:06:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76450862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定