Selenium - click on the link to open a new tab


tree 55

I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and continue with the original tab. Thanks in advance for your help!

Prageeth Saravanan
//Get Current Page 
String currentPageHandle = driver.getWindowHandle();                
linkToClick.click();        

//Add Logic to Wait till Page Load 

// Get all Open Tabs
ArrayList<String> tabHandles = new ArrayList<String>(driver.getWindowHandles());

String pageTitle = "ThePageTitleIhaveToCheckFor";
boolean myNewTabFound = false;

for(String eachHandle : tabHandles)
{
    driver.switchTo().window(eachHandle);
    // Check Your Page Title 
    if(driver.getTitle().equalsIgnoreCase(pageTitle))
    {
        // Report ur new tab is found with appropriate title 

        //Close the current tab
        driver.close(); // Note driver.quit() will close all tabs

        //Swithc focus to Old tab
        driver.switchTo().window(currentPageHandle);
        myNewTabFound = true;           
    }
}

if(myNewTabFound)
{
    // Report page not opened as expected       
}

Related


Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and con

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and con

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and need to validate the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and conti

Right click and save link to open in new tab

Dishu I added a custom right click menu using jquery in my project, now what I want to achieve is: -> If anyone right clicks on the link, the option will say "Open link in new tab", when that option is selected, it will open in a new tab with that link. -> Oth

Bind click event to open link in new tab

123 I'm trying to open all links in a modal in a new tab, manipulating the DOM is not as easy as I thought due to the limitations of using angular. Is there a way to bind a click event to a link and then make the click event open a new tab? I have something li

How to command click (open in new tab) in selenium

Joey I have an array of selenium.webdriver.remote.webelement.WebElements . In order to loop through the elements and keep the session, I need to open each element in a new tab, extract the data, and then close the tab. I see that there is a way to open a new t

Selenium 2: open link in new tab and close tab

Alps: I want to be able to open links in new tabs in Selenium 2. I also want to close the tab after I finish interacting with the page. How is this possible if I have WebElementa <a>tag? I'm using Selenium 2's Java API with the Firefox driver running on Firefo

Selenium 2: open link in new tab and close tab

Alps: I want to be able to open links in new tabs in Selenium 2. I also want to close the tab after I finish interacting with the page. How is this possible if I have WebElementa <a>tag? I'm using Selenium 2's Java API with the Firefox driver running on Firefo

Selenium 2: open link in new tab and close tab

Alps: I want to be able to open links in new tabs in Selenium 2. I also want to close the tab after I finish interacting with the page. How is this possible if I have WebElementa <a>tag? I'm using Selenium 2's Java API with the Firefox driver running on Firefo

"Open in new tab" click link does not open href value

99 encoder I am using Vue2 and I have and <a>markup . This works as expected with a left click (it's calling a method), but if I do a , which is the open value in this case, it 's wrong.href='#'@click.prevent="someMethod()" right click -> open in new tabhref W

Click the link to open the tab

Shakamar I want to open a tab when a user clicks an image link on the following website : http://m.theseolounge.co.uk/index.php/jadever-jad-jce-counting-scale.html The code for the tab is as follows: <ul class="tabs clearer"> <li id="tab-additional"><a href=

Open link in new tab

no mice here I need to open a link <a>in a new tab/window 's tab, do something there and then close it. html code: <body> <a id="uniqueid" href="somelink">I need to open this in a new tab</a> </body> python code: from selenium import webdriver driver = w

How to open a link in a new tab in a menu using Selenium in Java

Ashish Savaliya I am trying to open a link in a new Tab in Selenium Java, but the first time only one link opens, but when opening the second link the For Loop is giving an error, can anyone help me with this. Here is my code. public class Link_Open_In_New_Tab

How to open link in new tab using python and selenium

User 3820991 I want to open links found on a website in a new tab. I tried to open a new tab and passed the link URL to the motorists suggested here however, the new tab does not open at all. (There are some other suggestions on how to open new tabs, but none

How to open link in new tab using python and selenium

User 3820991 I want to open links found on a website in a new tab. I tried to open a new tab and passed the link URL to the motorists suggested here however, the new tab does not open at all. (There are some other suggestions on how to open new tabs, but none