How to right click on a link and open the link in a new tab via Java using Selenium


SBRoy:

Do I want to right click on " Forgotten Account"? The link is linked on the Facebook login page using Selenium, but it doesn't work.

I wanted to try send.Keys()after, contextClick()but the keypress happens on the page, not on the context menu.

package keyboardandmouseaction;

import java.awt.AWTException;
import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class testcase8 {
    public static void main(String[] args) throws AWTException, InterruptedException {

        System.out.println("Running keyboardandmouseactions > testcase8");

        System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();

        driver.manage().window().maximize();
        driver.get("https://www.facebook.com/");

        WebElement link=driver.findElement(By.xpath("//a[contains(text(),\"Forgotten account?\")]"));
        Actions a=new Actions(driver);

        // defective code start
        Action builder=a.moveToElement(link).contextClick(link).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build();
        // defective code end
        builder.perform();



        Set<String> windowid =driver.getWindowHandles();
        Iterator<String> itr =windowid.iterator();

        String mainwindow=itr.next();
        String childwindow=itr.next();
        System.out.println("The mainwindow id is "+mainwindow);
        System.out.println("The childwindow id is "+childwindow);
        driver.switchTo().window(childwindow);
        driver.get("http://demo.automationtesting.in/Alerts.html");
        driver.close();

}
}
Norayr Sargsyan:
WebDriver driver = new ChromeDriver();
driver.get("https://www.facebook.com/");
WebElement link=driver.findElement(By.xpath("//a[contains(text(),\"Forgotten account?\")]"));
Actions actions = new Actions(driver);

actions.keyDown(Keys.LEFT_CONTROL)
        .click(element)
        .keyUp(Keys.LEFT_CONTROL)
        .build()
        .perform();

ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tab.get(1));

}

Related


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

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

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

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

Customize right click anchor tag to open link in new tab

usman610 I have the following code to open a specific right click menu for <a>an element . On any link I right click and click to open in a new tab, it only opens the first link. Counters in java-script are related to my inability to do so. hrefI want to open

How to open a link in a new tab using JavaScript

ash I am working on a website where I have to open a URL from the backend. I am using c# now. My problem is that I want to open the link in a new tab instead of a new window. my code is here: string url = ppHref.ToString(); string newScript = "<script languag

How to open a link in a new tab using JavaScript

ash I am working on a website where I have to open a URL from the backend. I am using c# now. My problem is that I want to open the link in a new tab instead of a new window. my code is here: string url = ppHref.ToString(); string newScript = "<script languag

How to open a clicked link in a new tab via Tampermonkey?

Crab Nebula So I have what seems to be a simple question. I am trying to automatically open a specific link on a page using the following code: // ==UserScript== // @name AutoClicker // @include https://example.com/* // @require http://ajax.googleapis.co

How to open a clicked link in a new tab via Tampermonkey?

Crab Nebula So I have what seems to be a simple question. I am trying to automatically open a specific link on a page using the following code: // ==UserScript== // @name AutoClicker // @include https://example.com/* // @require http://ajax.googleapis.co

How to click a link via Selenium?

Tamim <a href="eventLog.cgi?command=0" target="content" class="Menu_titleFont">View Event Log</a> How to click "view event log"in selenium? I tried By.CssSelector("a[href^='eventLog.cgi?command=0']") But "NoSuchElementException was unhandled"an error occurre

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 click an AngularJS link using Java Selenium?

User 11885603 I am using Selenium WebDriver in Java. I've got most things done, but I'm stuck at one point, I have the following HTML code: <a ng-href="#/studyenrollments/new" ng-show="canCreate" class="btn btn-primary edit-btn" href="#/studyenrollments/new">N