在Selenium Webdriver中使用Ctrl +单击组合键打开一个新选项卡 [英] Opening a new tab using Ctrl + click combination in Selenium Webdriver
问题描述
我正在尝试使用ctrl,然后单击链接以在新标签页中将其打开。这在Chrome 58中可以正常运行。请找到以下代码:
I am attempting to use ctrl + click on a link to open it in a new tab. This is working fine in Chrome 58. Please find the code below:
action.keyDown(Keys.CONTROL).click(driver.findElement(By.xpath
("//section[@class='filmStrip__basic']//a[text()='En savoir
plus']"))).keyUp(Keys.CONTROL).build().perform();
我在IE,Firefox和Safari上使用相同的代码,但出现以下错误:
I am using the same code on IE, Firefox and Safari but getting the following error:
Firefox 54:该链接在同一标签中打开。
IE 11:什么也没发生..控件正在移至下一行
Safari:action.keyDown-Unrecognized命令上的异常
Firefox 54: The link is getting open in the same tab. IE 11: Nothing happening.. the control is moving to the next line Safari: exception on action.keyDown-Unrecognized command
与帮助有关
谢谢
推荐答案
您尝试单击< a>
标记内的链接,而不是使用 xpath
linkText
定位器。以下是示例代码,其中打开了URL http://www.google.com
,验证了页面标题
,使用 Actions
类单击 Gmail
链接以打开 https://accounts.google .com
。
As you are trying to click on a link which is within a <a>
tag, instead of xpath
you can use the linkText
locator. Here is the sample code with opens the url http://www.google.com
, verifies the Page Title
, uses Actions
class to click on the Gmail
link to open https://accounts.google.com
in a new tab.
String URL="http://www.google.com";
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get(URL);
System.out.println("Page Title is : "+driver.getTitle());
WebElement link = driver.findElement(By.linkText("Gmail"));
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.CONTROL).click(link).keyUp(Keys.CONTROL).build().perform();
您可以找到基于相关的 Python 如何使用Selenium Webdriver在同一窗口的新标签页中打开嵌入在主标签中Web元素中的链接
这篇关于在Selenium Webdriver中使用Ctrl +单击组合键打开一个新选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!