如何使用actions类打开相邻窗口或选项卡中的链接(上下文单击元素 - > sendkeys ArrowDown - > sendkeys Enter)

问题描述 投票:1回答:2

我想在新窗口中打开应用程序的链接。使用actions类是在我当前所在的同一窗口中打开链接。

我已尝试使用具有以下代码的Actions类与chrome驱动程序和具有多个应用程序的firefox驱动程序但没有工作。

System.setProperty("webdriver.gecko.driver", "D:\\MySpace\\AmozonEcomm\\Dependencies\\geckodriverv21.exe");
WebDriver driver = new FirefoxDriver(); //Creating an instance of Chrome driver
driver.manage().window().maximize();
driver.get("http://automate-apps.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

Actions action = new Actions(driver);
action.contextClick(driver.findElement(By.partialLinkText("Selenium Questions"))).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();

我的期望是链接应该在新窗口中打开,因为我试图在上下文单击后选择“在新窗口中打开链接”选项。但是evrytime链接在同一窗口中打开了。

java selenium selenium-webdriver webdriver webdriverwait
2个回答
1
投票

以下是在新窗口(而非制表符)中打开链接的两个选项。

WebElement el = driver.findElement(By.partialLinkText("Selenium Questions"));
Actions action = new Actions(driver);

contextClick - contextClick()可能是反复无常的,在某些环境中它只是拒绝实现与其他环境相同的结果。 如果您想尝试使用它,请发送“w”键 - 这是“在新窗口中打开”的快捷方式:

action.contextClick(el).perform();
action.sendKeys("w").perform();  // in two steps

按住Shift键单击 - 新窗口的快捷方式是按住Shift键单击,而不是按住Ctrl键单击新选项卡。这种方法比contextClick()具有更高的重复性 - 只要浏览器支持它。

action.keyDown(Keys.SHIFT).click(el).keyUp(Keys.SHIFT).build().perform();

1
投票

要在相邻选项卡/窗口中打开带有文本的链接作为Selenium Questions,您可以使用以下解决方案:

  • 代码块: System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("http://automate-apps.com/"); WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Selenium Questions"))); new Actions(driver).keyDown(Keys.CONTROL).click(elem).keyUp(Keys.CONTROL).build().perform();
  • 浏览器快照:

newTAB

© www.soinside.com 2019 - 2024. All rights reserved.