简体   繁体   English

在新标签页硒C#中打开链接

[英]Open link in new tab selenium c#

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(); 我在列表videoLinks = getVideoUrls();有一百个视频URL videoLinks = getVideoUrls(); and now what I need is to execute these videos 5 at a time. 现在我需要一次执行这些视频5。

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. 如果按照上述方法进行操作,那么对于所有视频,我都必须创建一个新的ChromeDriver对象。 I want to use single chrome browser object. 我想使用单个Chrome浏览器对象。

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. 我已经用Google搜索,但是找不到解决方案,因此想寻求帮助。

Try this: 尝试这个:

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

You can use CurrentWindowHandle to find current tab. 您可以使用CurrentWindowHandle查找当前选项卡。

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'); 要检查这一点,请在浏览器中打开js控制台,然后尝试执行命令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. 您还可以使用特定设置创建chrome驱动程序。 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));

Here is a simple solution for open a new tab in seleneium c#: 这是在硒c#中打开新标签页的简单解决方案:

driver.Url = "http://www.gmail.net";
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.open();");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM