Artem
Artem

Reputation: 23

Selenium and new tab

How i can open link on new tab in selenium webdriver ( firefox ) ?

<a href='/test' id='test'>Link</a>

driver.find_element_by_id('test').click()

Upvotes: 2

Views: 1858

Answers (2)

sunil dewna
sunil dewna

Reputation: 11

package com.crm.qa.BaseTest;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class NewTabChrome {

    public static void main(String[] args) throws AWTException {

        System.setProperty("webdriver.chrome.driver", 
                "C:/Users/sunil/Downloads/chromedriver_win32 (2)/chromedriver.exe");

        WebDriver driver = new ChromeDriver();//open browser
        driver.manage().window().maximize();//browser maximize
        driver.get("http://www.google.com");//open google 

        //open new tab 
        for(int i = 0; i<=1;i++){
        Robot rob = new Robot();
        rob.keyPress(KeyEvent.VK_CONTROL);
        rob.keyPress(KeyEvent.VK_T);
        rob.keyRelease(KeyEvent.VK_CONTROL);
        rob.keyRelease(KeyEvent.VK_T);
        ArrayList<String> tabs1 = new ArrayList<String> (driver.getWindowHandles());
        //Switch to new tab
        driver.switchTo().window((String) tabs1.get(i));
    }
        //open facebook
        driver.get("http://facebook.com");
        driver.quit();
    }
}

Upvotes: 1

MK_Dev
MK_Dev

Reputation: 3333

It looks like your best bet at the moment is to inject an anchor tag into the page. You'll need to adapt this to python, but it should be relatively straight forward: https://stackoverflow.com/a/9122450/39843

Upvotes: 1

Related Questions