使用Selenium C#打开新标签中的链接 [英] Open link in New tab using Selenium C# for chrome

查看:131
本文介绍了使用Selenium C#打开新标签中的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

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



我在List< string>中有一百个视频网址videoLinks = getVideoUrls();

现在我需要的是一次执行这些视频。



 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);







它只添加一个新标签,但没有在那里打开链接。

请让我知道我应该怎么回事。我用谷歌搜索但找不到我的解决方案,所以想找你的帮忙。



我很感激你的回复。

谢谢

解决方案

动作动作=新动作(_driver);

action.KeyDown(Keys.Control).MoveToElement(body).Click ()。执行();



这将在新选项卡中打开IwebElement主体。


我会添加这块代码:



动作动作=新动作(_driver); 
action.KeyDown(Keys.Control).MoveToElement(body).Click()。Perform();





将不释放键盘中的CTRL键如果这段代码在循环中运行,可能导致在同一窗口中打开新选项卡。



所以正确的版本是:



动作动作=新动作(_driver ); 
action.KeyDown(Keys.Control).MoveToElement(body).Click()。 KeyUp(Keys.Control )。执行();


Hello,
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<string> 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 from you geeks.

I would appreciate your response.
Thanks

解决方案

Actions action = new Actions(_driver);
action.KeyDown(Keys.Control).MoveToElement(body).Click().Perform();

This will open IwebElement body in new tab.


I would add that this piece of code:

Actions action = new Actions(_driver);
action.KeyDown(Keys.Control).MoveToElement(body).Click().Perform();



will not release CTRL key in the keyboard what can cause openning new tab in same window if this piece of code is running in a loop.

So right version is:

Actions action = new Actions(_driver);
action.KeyDown(Keys.Control).MoveToElement(body).Click().KeyUp(Keys.Control).Perform();


这篇关于使用Selenium C#打开新标签中的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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