How to right click on a link and open link in new tab using selenium webdriver

Script1:-

package selenium_examples;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

WebElement glink = driver.findElement(By.linkText("About"));
                                Actions action= new Actions(driver);
action.contextClick(glink).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();


}
}


 
The above script clicks on 'About' link in google page and opens 'About google' page in new tab.

right click on link in webdriver


contextClick()-Performs a context-click at the current mouse location.

contextClick(WebElement onElement)-Performs a context-click at middle of the given element

Script2:-

Below script clicks on 'About' link in google page and opens 'About google' page in new window.

package selenium_examples;



import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

WebElement glink = driver.findElement(By.linkText("About"));
                Actions action= new Actions(driver);
action.contextClick(glink).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();


}
}


 

No comments:

Related Posts Plugin for WordPress, Blogger...

Fun and Knowledge