如何在Selenium C#中切换Chrome上的标签? [英] How to switch tabs on Chrome in Selenium C#?

查看:551
本文介绍了如何在Selenium C#中切换Chrome上的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用:


  • Chrome 55.0.2

  • Selenium WebDriver 3.0.1 with ChromeDriver

  • C#4.6.1(VS2015社区版)

我正在编写自动化代码打开Chrome,打开标签页,然后导航到主页面网址。此主页面包含我解析以生成辅助页面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

在我生成此辅助页面网址后,我想打开另一个标签页,让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 (切换回标签1在Chrome中)

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

我也会对在新的Chrome标签页中导航并加载辅助页面网址后的解决方案感到满意,然后自动切换回主页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标签中打开与主页相同的生成网址:

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#\Chrome \WebDriver命令来打开一个网址Chrome中的新标签 - 它将始终使用当前标签。

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

我能够使用javascript在新标签页中打开网址找到代码。看起来像这样:

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将重点放在辅助页面\ tab上。此外,没有错误消息。从我在网上看到的这个命令应该切换标签。

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\tab. 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会将其重点放在辅助页面\ tab上。此外,没有错误消息。

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\tab. 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.

推荐答案


作为最后的努力,我正在寻找一个可以使用的javascript命令用IJavaScriptExecutor切换标签 - 到目前为止没有运气。

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上的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆