如何在 Selenium C# 中切换 Chrome 上的选项卡?

How to switch tabs on Chrome in Selenium C#?(如何在 Selenium C# 中切换 Chrome 上的选项卡?)
本文介绍了如何在 Selenium C# 中切换 Chrome 上的选项卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用:

  • Chrome 55.0.2
  • 带有 ChromeDriver 的 Selenium WebDriver 3.0.1
  • C# 4.6.1(VS2015 社区版)

我正在编写可打开 Chrome、打开选项卡并导航到主页 URL 的自动化代码.此主页包含我解析以生成辅助页面 URL 的信息.我的问题是我无法编写在辅助页面加载后切换回主页选项卡的代码,也无法编写永远不会离开主页的代码.我将解释我正在追求的两个选项:

I am writing automation code that opens Chrome, opens a tab, and navigates to a main page URL. This main page has information that I parse to generate a secondary page URL. My issue is that I cannot write code that will switch back to the main page tab after the secondary page loads, nor can I write code that never leaves the main page. I will explain the two options I am pursuing:

选项 #1 - 行为类似于 ctrl-click

生成此辅助页面 url 后,我想打开另一个选项卡,让 Chrome 导航到该 url,但不离开 Chrome 中的主页选项卡.这类似于链接上的 ctrl-click.

After I generate this secondary page url, I would like to open another tab, have Chrome navigate to the url, but not leave the main page tab in Chrome. This is similar to ctrl-click on a link.

选项 #2 - 行为类似于 ctrl-1(在 Chrome 中切换回选项卡 1)

Option #2 - behave like ctrl-1 (switch back to tab 1 in Chrome)

我也会对这样一种解决方案感到满意,即在新的 Chrome 选项卡中导航和加载辅助页面 url 后,自动化将切换回 Chrome 中的主页选项卡.这类似于在 Chrome 获得焦点时在键盘上键入 ctrl-1.

I would also be satisfied with a solution that after navigating and loading the secondary page url in a new Chrome tab, the automation will then switch back to the main page tab in Chrome. This is similar to typing ctrl-1 on a keyboard while Chrome has focus.

选项 #3 - 其他?

我也会对任何其他能够实现我正在寻找的解决方案感到满意(不引入任何新技术层).

I would also be satisified with any other solution that accomplishes what I'm looking for (without introducing any new technology layer).

一些背景信息 - 导航

我首先尝试了以下方法,但它未能满足我的要求,因为它在 Chrome 选项卡中打开生成的 url 作为主页:

I tried the following approach first, but it fails my requirement because it opens the generated url in the same Chrome tab as the main page:

Session.Driver.Navigate().GoToUrl(generatedUrl);

我还没有发现直接"C#ChromeWebDriver 命令可以在 Chrome 的新标签页中打开 url - 它总是使用当前标签页.

I have not discovered a "direct" C#ChromeWebDriver command to open a url in a new tab in Chrome - it will always use the current tab.

我能够使用 javascript 在新选项卡中打开 url 找到代码.它看起来像这样:

I was able to find code using javascript that opens a url in a new tab. It looks like this:

// open a new tab and navigate to generated url:
IJavaScriptExecutor jscript = Session.Driver as IJavaScriptExecutor;
jscript.ExecuteScript(string.Format("window.open('{0}', '_blank');", generatedUrl));

在执行 javascript 之后,二级页面被打开并且是 Chrome 中的活动标签.以下是我在 Chrome 中导航回主页选项卡的尝试:

And after executing the javascript, the secondary page is opened and is the active tab in Chrome. Here are my attempts to navigate back to the main page tab in Chrome:

我的尝试

我当然尝试了很多方法来切换标签等.在这里,它们都失败了:

I of course tried many ways to switch tabs, etc. Here they are, all failures:

尝试 #1:SwitchTo()

我可以使用 Session.Driver.WindowHandles 访问每个窗口句柄.当我调试以下命令时,我看到第一个窗口句柄确实是主页;但是,该命令什么也不做.没有选项卡更改.Chrome 将注意力集中在二级页面标签上.此外,没有错误消息.根据我在网上阅读的内容,该命令应该切换选项卡.

I am able to access each window handle using Session.Driver.WindowHandles. When I debug the following command I see that the first window handle is indeed the main page; however, the command does nothing. No tab changes. Chrome keeps its focus on the secondary page ab. Also, no error message. From what I've read online this command was supposed to switch the tabs.

Session.Driver.SwitchTo().Window(Session.Driver.WindowHandles.First());

尝试 #2:发送密钥 - 操作

如果这种方法有效,那将是非常棒的.同样,以下代码没有任何影响,Chrome 将其重点放在辅助页面标签上.此外,没有错误消息.

This approach would be really great if it worked. Again, the following code has no impact and Chrome keeps it focus on the secondary page ab. Also, no error message.

    Actions action = new Actions(Session.Driver);
    action.SendKeys(Keys.Control + "1").Build().Perform();

<小时>

作为最后的努力,我目前正在寻找一个可以与 IJavaScriptExecutor 一起使用的 JavaScript 命令来切换选项卡 - 到目前为止还没有运气.


As a last ditch effort, I am currently searching for a JavaScript command that I can use with IJavaScriptExecutor to switch tabs - no luck so far.

推荐答案

作为最后的努力,我目前正在寻找一个可以与 IJavaScriptExecutor 一起使用来切换选项卡的 javascript 命令 - 到目前为止还没有运气.

As a last ditch effort, I am currently searching for a javascript command that I can use with IJavaScriptExecutor to switch tabs - no luck so far.

我正在为您提供一个使用 JavaScript 在选项卡之间切换的技巧.这不是一个完美的解决方案,但您可以用作替代解决方案.你应该尝试:-

I'm providing you a trick using JavaScript to switching between tabs. This is not a perfect solution but you can use as alternate solution. You should try as :-

//Firstly try to switch window as you're doing 
Session.Driver.SwitchTo().Window(Session.Driver.WindowHandles.First());

//Now execute a script which would popup the alert box on current window
IJavaScriptExecutor jscript = Session.Driver as IJavaScriptExecutor;
jscript.ExecuteScript("alert('Switch tab')");

//Now you can see alert would focused on desired tab
//Now you can accept this alert and do further steps on this tab
IAlert alert = Session.Driver.SwitchTo().Alert();
alert.Accept();

注意:- 如果您在尝试接受警报时收到警报异常并且无法找到警报,您应该等到 JavaScript 触发器和警报框打开.

Note:- If you're getting alert exception when trying to accept alert and unable to find alert, You should wait until JavaScript trigger and alert box open.

这篇关于如何在 Selenium C# 中切换 Chrome 上的选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)