在新选项卡中打开链接selenium c# [英] Open link in new tab selenium c#

查看:561
本文介绍了在新选项卡中打开链接selenium c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序来运行我的网站上列出的视频用于测试目的,这里我需要的是在同一浏览器窗口的不同选项卡中运行视频。

我在List videoLinks = getVideoUrls();
中有100个视频网址,需要每次执行5个视频。

  ChromeDriver driver = new ChromeDriver(); 
driver.Navigate()。GoToUrl(https://www.withoutabox.com+ videoLink);

如果按照上述方式创建所有视频​​,我必须创建一个新的ChromeDriver对象。我想使用单一的Chrome浏览器对象。



我试过了这个

  IWebElement body = driver.FindElement(By.TagName(body)); 
body.SendKeys(Keys.Control +t);

它只添加一个新标签,但不打开链接。
请让我知道我该如何解决它。我试过了:

解决方案

试试这个:

  public void SwitchToTab(object pageId)
{
webDriver.SwitchTo()。Window(pageId.ToString ());
}

您可以使用CurrentWindowHandle查找当前选项卡。 b

  webDriver.CurrentWindowHandle; 

对于您的场景,我使用该代码:

  public IPageAdapter OpenNewTab(string url)
{
var windowHandles = webDriver.WindowHandles;
scriptExecutor.ExecuteScript(string.Format(window.open('{0}','_blank');,url));
var newWindowHandles = webDriver.WindowHandles;
var openedWindowHandle = newWindowHandles.Except(windowHandles).Single();
webDriver.SwitchTo()。Window(openedWindowHandle);
返回新的SeleniumPage(webDriver);
}

更新
$ b

打开窗口创建新的弹出窗口。默认情况下,该选项可以被浏览器设置阻止。手动禁用弹出式窗口阻止功能。



若要检查此操作,请在浏览器中打开js控制台并尝试执行命令window.open(' http://facebook.com ','_blank');



如果新窗口比everythng成功打开成功。



您也可以创建具有特定设置的chrome驱动程序。这里是我的代码:

  var chromeDriverService = ChromeDriverService.CreateDefaultService(); 
var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference(profile.default_content_settings.popups,0);
返回新的ChromeDriver(chromeDriverService,chromeOptions,TimeSpan.FromSeconds(150));


I am writing a program to run videos listed on my site for testing purpose and here what I need is to run videos in different tabs of the same browser window.

I have hundred video urls in the List videoLinks = getVideoUrls(); and now what I need is to execute these videos 5 at a time.

ChromeDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.withoutabox.com" + videoLink);

If I go the above way then for all videos I will have to create a new ChromeDriver object. I want to use single chrome browser object.

I have tried this

IWebElement body = driver.FindElement(By.TagName("body"));
body.SendKeys(Keys.Control + "t");

it only adds a new tab but not open a link there. Please let me know how should I go around it. I have googled but couldn't find my solution so thought to ask for help.

解决方案

Try this:

public void SwitchToTab(object pageId)
{
    webDriver.SwitchTo().Window(pageId.ToString());
}

You can use CurrentWindowHandle to find current tab.

webDriver.CurrentWindowHandle;

For your scenario I'm using that code:

public IPageAdapter OpenNewTab(string url)
{
    var windowHandles = webDriver.WindowHandles;
    scriptExecutor.ExecuteScript(string.Format("window.open('{0}', '_blank');", url));
    var newWindowHandles = webDriver.WindowHandles;
    var openedWindowHandle = newWindowHandles.Except(windowHandles).Single();
    webDriver.SwitchTo().Window(openedWindowHandle);
    return new SeleniumPage(webDriver);
}

Update

Window open create new popup. By default this option can be blocked by browser settings. Disable popup blocking in your browser manually.

To check this, open js console in your browser and try to execute command window.open('http://facebook.com', '_blank');

If new window open successfully than everythng is OK.

You can also create your chrome driver with specific setting. Here is my code:

var chromeDriverService = ChromeDriverService.CreateDefaultService();
var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("profile.default_content_settings.popups", 0);
return new ChromeDriver(chromeDriverService, chromeOptions, TimeSpan.FromSeconds(150));

这篇关于在新选项卡中打开链接selenium c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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