在新标签页中打开链接适用于Firefox,但不适用于使用Selenium的Chrome浏览器

时间:2021-09-29 20:35:41

I have a test where I need to open a link in a new tab. This must work in Firefox and Chrome. I first tried it with the Gmail link on the Google page.

我有一个测试,我需要在新选项卡中打开一个链接。这必须适用于Firefox和Chrome。我首先尝试使用Google页面上的Gmail链接。

On Firefox it works perfectly, Gmail is opened in a new tab. But on Chrome the Gmail page is opened in the same window and the menu remains open after right click. Has anyone come across this problem?

在Firefox上它完美运行,Gmail在新标签页中打开。但在Chrome上,Gmail页面会在同一窗口中打开,右键单击后菜单仍然保持打开状态。有人遇到过这个问题吗?

Beneath is my sample code.

下面是我的示例代码。

Firefox code:

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 code:

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

3 个解决方案

#1


I've encountered the same issue. Apparently, ARROW_DOWN won't work, so I tried using the keys combination and it works for me. The code is as follows:

我遇到了同样的问题。显然,ARROW_DOWN不起作用,所以我尝试使用键组合,它适用于我。代码如下:

1) opening in a new tab with focus still on current tab

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) opening in a new tab and moving to the new tab

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

Hope this helps.

希望这可以帮助。

#2


Yes, you can do that easily using Selenium. Use the Key commands (Ctrl +T) to open a new tab and then use Ctrl +Tab (Ctrl +\t) command to switch to newly opened tab and perform whatever is necessary. It would go something like this

是的,您可以使用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");

This will work if the browser only has two tabs open, because then it will use the Tab key to move between the two open tabs.

如果浏览器只打开了两个选项卡,这将起作用,因为它将使用Tab键在两个打开的选项卡之间移动。

Hope this helps.

希望这可以帮助。

#3


For Chrome try this:

对于Chrome,试试这个:

Actions a = new Actions(webdriver);
WebElement e = webdriver.findElement(By.xpath(your_path));
a.moveToElement(e).keyDown(Keys.CONTROL).click().build().perform();

#1


I've encountered the same issue. Apparently, ARROW_DOWN won't work, so I tried using the keys combination and it works for me. The code is as follows:

我遇到了同样的问题。显然,ARROW_DOWN不起作用,所以我尝试使用键组合,它适用于我。代码如下:

1) opening in a new tab with focus still on current tab

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) opening in a new tab and moving to the new tab

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

Hope this helps.

希望这可以帮助。

#2


Yes, you can do that easily using Selenium. Use the Key commands (Ctrl +T) to open a new tab and then use Ctrl +Tab (Ctrl +\t) command to switch to newly opened tab and perform whatever is necessary. It would go something like this

是的,您可以使用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");

This will work if the browser only has two tabs open, because then it will use the Tab key to move between the two open tabs.

如果浏览器只打开了两个选项卡,这将起作用,因为它将使用Tab键在两个打开的选项卡之间移动。

Hope this helps.

希望这可以帮助。

#3


For Chrome try this:

对于Chrome,试试这个:

Actions a = new Actions(webdriver);
WebElement e = webdriver.findElement(By.xpath(your_path));
a.moveToElement(e).keyDown(Keys.CONTROL).click().build().perform();