gpt4 book ai didi

javascript - 在 Selenium Webdriver 中使用 Ctrl + 单击组合打开一个新选项卡

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:02:49 26 4
gpt4 key购买 nike

我正在尝试使用 ctrl + 单击链接在新选项卡中打开它。这在 Chrome 58 中运行良好。请找到以下代码:

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 上使用相同的代码,但出现以下错误:

Firefox 54:链接在同一个选项卡中打开。IE 11:什么都没发生.. 控件正在移动到下一行Safari:action.keyDown 异常 - 无法识别的命令

也欢迎与任何一种浏览器相关的帮助。

谢谢

最佳答案

当您尝试单击 <a> 中的链接时标签,而不是 xpath你可以使用 linkText定位器。这是打开 url http://www.google.com 的示例代码, 验证 Page Title , 使用 Actions类点击Gmail打开链接https://accounts.google.com在新标签页中。

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

You can find a relevant Python based solution in How to open a link embed in web element in the main tab, in a new tab of the same window using Selenium Webdriver

关于javascript - 在 Selenium Webdriver 中使用 Ctrl + 单击组合打开一个新选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46201035/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com