Tabs opening in selenium -Multiple tabs via (Keyschord)/Single tab using Context click and Robot class
I'll be showcasing you opening multiple tabs via selenium by 2 different method
1. the below code is used for opening multiple tabs in amazon using the Command(mac)/Control(win) + T method using Keychord, you can also use send keys using Action class but this approach does not always work , due to send keys issues with browser.
2.you have another method using "Robot class" Which is explained later steps
Let's discuss first about opening multiple tans in same browser using Keys.chord Command(mac)+T method
public class MultipleTabs {
WebDriver driver;
@Test
public void test2() {
System.setProperty("Webdriver.chrome.driver","chromedriver");
WebDriver driver= new ChromeDriver();
driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
driver.get("https://www.amazon.in/");
driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
//xpath of the frame
//Multitabs.findElements(By.tagName("a")).size()) give you all the links present under the frame
//"Make money with us" for that use findelement(s) instead of findelement
WebElement Multitabs = driver.findElement(By.xpath("//div[@class='navFooterLinkCol navAccessibility'][3]"));
List <WebElement> links = Multitabs.findElements(By.tagName("a"));
System.out.println(links.size());
//for iterating the loop for the links
for(int i=0;i< links.size();i++)
{
//To open links in new tab
//Keys.command is used in MAC , for windows it would be Keys.control,Keys.Enter
String opentab = Keys.chord(Keys.COMMAND,Keys.ENTER);
// open all links one by one in new tab
links.get(i).sendKeys(opentab);
}
}
}
You can check the result here in eclipse
Amazon - opening multiple tabs
Opening a single tabs by doing right click using Contextclick
WebDriver driver;
@Test
public void test2() throws AWTException {
System.setProperty("Webdriver.chrome.driver","chromedriver");
WebDriver driver= new ChromeDriver();
driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
driver.get("https://www.google.com/");
WebElement w = driver.findElement(By.xpath("//div[@class='navFooterLinkCol navAccessibility'][3]"));
w.sendKeys(Keys.chord(Keys.COMMAND,Keys.ENTER));
here we are going to perform mouseover over "Acccout and list" and select "Discover your style "
then right click on the "discover your style" and opening this on a new tab
public class SingleTabs {
WebDriver driver;
@Test
public void test2() throws AWTException {
System.setProperty("Webdriver.chrome.driver","chromedriver");
WebDriver driver= new ChromeDriver();
driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
driver.get("https://www.amazon.co.in");
driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
WebElement abc = driver.findElement(By.id("nav-link-accountList"));
Actions act = new Actions(driver);
act.moveToElement(abc).build().perform();
//getting total number of links in the hover over window frame
WebElement ad = driver.findElement(By.xpath("//div[@id='nav-al-container']"));
List <WebElement> links = ad.findElements(By.tagName("a"));
System.out.println(links.size());
//19 links present
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
WebElement ac = driver.findElement(By.linkText("Discover Your Style"));
act.moveToElement(ac).build().perform();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//To perform right click on the menu bar using context click and click the first option
act.contextClick(ac).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
//To make sure above function worked well
System.out.println("Open link button Clicked");
}
}
Check the code and the final screenshot after the run
Opening a single tabs by doing right click using robot class
public class SingleTabs {
WebElement ac = driver.findElement(By.linkText("Baby Wish List"));
act.moveToElement(ac).build().perform();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Robot bot = new Robot();driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
//Right click
bot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
bot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
System.out.println("Browse button Clicked");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//selecting first option of the right click which is back
WebDriver driver;
@Test
public void test2() throws AWTException {
System.setProperty("Webdriver.chrome.driver","chromedriver");
WebDriver driver= new ChromeDriver();
driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
driver.get("https://www.amazon.co.in");
driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
WebElement abc = driver.findElement(By.id("nav-link-accountList"));
Actions act = new Actions(driver);
act.moveToElement(abc).build().perform();
// create a robot class
bot.keyPress(KeyEvent.VK_DOWN);
bot.keyRelease(KeyEvent.VK_DOWN);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
bot.keyPress(KeyEvent.VK_ENTER);
bot.keyRelease(KeyEvent.VK_ENTER);
System.out.println("Open link button Clicked");
}
}
Comments
Post a Comment