在新选项卡中打开 link 适用于 Firefox,但不适用于使用 Selenium 的 Chrome 浏览器
Opening a link in a new tab is working for Firefox but not working for Chrome browser using Selenium
我有一个测试需要在新选项卡中打开 link。这必须在 Firefox 和 Chrome 中有效。我首先在 Google 页面上使用 Gmail link 尝试了它。
在 Firefox 上运行完美,Gmail 在新选项卡中打开。
但在 Chrome 上,Gmail 页面在相同的 window 中打开,右键单击后菜单保持打开状态。有人遇到过这个问题吗?
下面是我的示例代码。
Firefox 代码:
FirefoxProfile myprofile;
ProfilesIni profile = new ProfilesIni();
myprofile = profile.getProfile("SeleniumAuto");
WebDriver driver = new FirefoxDriver(myprofile);
driver.get("http://www.google.com");
driver.manage().window().maximize();
Actions a = new Actions(driver);
WebElement e = driver.findElement(By.xpath("/html/body/div/div[3]/div[1]/div/div/div/div[1]/div[2]/a"));
a.moveToElement(e);
a.contextClick(e).sendKeys(Keys.ARROW_DOWN)
.sendKeys(Keys.ENTER).build().perform();
Chrome代码:
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com");
driver.manage().window().maximize();*/
Actions a = new Actions(driver);
WebElement e = driver.findElement(By.xpath("/html/body/div/div[3]/div[1]/div/div/div/div[1]/div[2]/a"));
a.moveToElement(e);
a.contextClick(e).sendKeys(Keys.ARROW_DOWN)
.sendKeys(Keys.ENTER).build().perform();
我遇到了同样的问题。显然,ARROW_DOWN 不起作用,所以我尝试使用组合键,它对我有用。代码如下:
1) 在新标签页中打开,焦点仍位于当前标签页
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(your_path)));
actions.keyDown(Keys.CONTROL).perform();
driver.findElement(By.xpath(your_path)).click();
actions.keyUp(Keys.CONTROL);
2) 在新标签页中打开并移动到新标签页
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(your_path)));
actions.keyDown(Keys.CONTROL).perform();
actions.keyDown(Keys.SHIFT).perform();
driver.findElement(By.xpath(your_path)).click();
actions.keyUp(Keys.SHIFT);
actions.keyUp(Keys.CONTROL);
希望对您有所帮助。
是的,您可以使用 Selenium 轻松做到这一点。使用键盘命令 (Ctrl +T) 打开一个新选项卡,然后使用 Ctrl +Tab (Ctrl +\t) 命令切换到新打开的选项卡并执行任何必要的操作。它会像这样
//open a new tab
WebElement e= driver.findElement(By.cssSelector(abc)).sendKeys(Keys.Control + "t");
//switch control to new tab
e.sendKeys(Keys.Control + "\t");
{
perform some function here
enter code here
}
//switch back to old tab
e.sendKeys(Keys.Control + "\t");
如果浏览器只打开了两个选项卡,这将起作用,因为它将使用 Tab 键在两个打开的选项卡之间移动。
希望对您有所帮助。
对于 Chrome 试试这个:
Actions a = new Actions(webdriver);
WebElement e = webdriver.findElement(By.xpath(your_path));
a.moveToElement(e).keyDown(Keys.CONTROL).click().build().perform();
我有一个测试需要在新选项卡中打开 link。这必须在 Firefox 和 Chrome 中有效。我首先在 Google 页面上使用 Gmail link 尝试了它。
在 Firefox 上运行完美,Gmail 在新选项卡中打开。 但在 Chrome 上,Gmail 页面在相同的 window 中打开,右键单击后菜单保持打开状态。有人遇到过这个问题吗?
下面是我的示例代码。
Firefox 代码:
FirefoxProfile myprofile;
ProfilesIni profile = new ProfilesIni();
myprofile = profile.getProfile("SeleniumAuto");
WebDriver driver = new FirefoxDriver(myprofile);
driver.get("http://www.google.com");
driver.manage().window().maximize();
Actions a = new Actions(driver);
WebElement e = driver.findElement(By.xpath("/html/body/div/div[3]/div[1]/div/div/div/div[1]/div[2]/a"));
a.moveToElement(e);
a.contextClick(e).sendKeys(Keys.ARROW_DOWN)
.sendKeys(Keys.ENTER).build().perform();
Chrome代码:
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com");
driver.manage().window().maximize();*/
Actions a = new Actions(driver);
WebElement e = driver.findElement(By.xpath("/html/body/div/div[3]/div[1]/div/div/div/div[1]/div[2]/a"));
a.moveToElement(e);
a.contextClick(e).sendKeys(Keys.ARROW_DOWN)
.sendKeys(Keys.ENTER).build().perform();
我遇到了同样的问题。显然,ARROW_DOWN 不起作用,所以我尝试使用组合键,它对我有用。代码如下:
1) 在新标签页中打开,焦点仍位于当前标签页
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(your_path)));
actions.keyDown(Keys.CONTROL).perform();
driver.findElement(By.xpath(your_path)).click();
actions.keyUp(Keys.CONTROL);
2) 在新标签页中打开并移动到新标签页
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(your_path)));
actions.keyDown(Keys.CONTROL).perform();
actions.keyDown(Keys.SHIFT).perform();
driver.findElement(By.xpath(your_path)).click();
actions.keyUp(Keys.SHIFT);
actions.keyUp(Keys.CONTROL);
希望对您有所帮助。
是的,您可以使用 Selenium 轻松做到这一点。使用键盘命令 (Ctrl +T) 打开一个新选项卡,然后使用 Ctrl +Tab (Ctrl +\t) 命令切换到新打开的选项卡并执行任何必要的操作。它会像这样
//open a new tab
WebElement e= driver.findElement(By.cssSelector(abc)).sendKeys(Keys.Control + "t");
//switch control to new tab
e.sendKeys(Keys.Control + "\t");
{
perform some function here
enter code here
}
//switch back to old tab
e.sendKeys(Keys.Control + "\t");
如果浏览器只打开了两个选项卡,这将起作用,因为它将使用 Tab 键在两个打开的选项卡之间移动。
希望对您有所帮助。
对于 Chrome 试试这个:
Actions a = new Actions(webdriver);
WebElement e = webdriver.findElement(By.xpath(your_path));
a.moveToElement(e).keyDown(Keys.CONTROL).click().build().perform();