Struggling to target an element post right-click action with Selenium

I attempted to use this code, but encountered an issue where it was not focusing on a new tab. Despite right-clicking, the program did not shift focus to a new tab and no error was thrown.

public class AM1 {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        driver.get("https://jira.dematic.com/secure/Dashboard.jspa");

        Thread.sleep(2000);
        WebElement taskElement = driver.findElement(By.xpath("//a[text()='Create']"));
        Actions actions = new Actions(driver);

        // Right click on element
        actions.contextClick(taskElement).perform();

        actions.sendKeys(Keys.ARROW_DOWN).perform();

    }
}

Answer №1

Unfortunately, it is not possible to navigate the context menu using key commands for any element on the page.

Based on the code provided, it appears that you are trying to open the URL of a link in a new window. This can be achieved by opening a new window with the URL of the link element.

There are essentially two methods to accomplish this: utilizing Javascript or creating a composite action involving modifier keys when clicking a link.

It is important to note that both methods may not work with javascript: URLs or fragment-only URLs with click event handlers attached.

Javascript

This method involves using a Javascript executor context to execute window.open() with the URL extracted from the link element.

// Execute a Javascript snippet
((JavascriptExecutor) d).executeScript("return window.open('"+ addoftask.getAttribute("href") +"', 'newtask');");
// Selenium does not automatically switch to new windows
d.switchTo().window("newtask");

More information on JavascriptExecutor can be found on SeleniumHQ.

Composite Action

This approach involves creating an action that presses the modifier key CTRL to open the link in a new tab. It also utilizes getWindowHandles() to retrieve a set of open windows (including tabs) and switches to the second one.

// Click with CTRL as modifier for new window.
a.keyDown(Keys.CONTROL).click(addoftask).perform();
// Switch to the second window
d.switchTo().window((String) d.getWindowHandles().toArray()[1]);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Execute a task in jQuery once the toggle() function has finished

I have a scroll functionality, and I want to perform an action after the toggle event is completed. The problem I'm facing is that my function gets triggered multiple times because it runs on every scroll. Pseudo flow: // Hide div if visible on scro ...

Prevent a child DIV from adopting the style of its parent page

Imagine a scenario where you have a webpage with its own unique stylesheet. You decide to include a popup or modal at the end of the page using Javascript, but you want this popup to have its own distinct style. Since the content within the modal is dynami ...

Tips for leveraging JSON for state management in React?

I am working with JSON data and utilizing SetState in the constructor { index:index, key: key, all: { 0: {name:'qwerty', id:'1', surname:'foo'} 1: {name:'bar', id:'2', surname ...

I have created an Express.js application. Whenever I visit a page, I consistently need to refresh in order for the variables to appear correctly

Hello, I'm seeking some assistance. Despite my efforts in searching for a solution, I have not been successful in finding one. I've developed an application using Express.js that includes a basic form in jade. The intention is to display "Yes" i ...

Nexus 3 Proxy for creating a virtual maven 2 repository

We recently made the transition from Nexus 2 to Nexus 3, and we are encountering an issue where we still need to support some artifacts uploaded by projects that are still using Maven 1. In Nexus 2, I set up a Maven 1 repository (N2-P-M1) and configured a ...

Experiencing difficulty connecting to local PostgreSQL on Heroku with Node.js

My goal is to set up a node.js app with postgreSQL db that works smoothly in the production environment. After successfully using the CLI command heroku pg:pull ..., I encountered an issue when running my application locally (foreman start). The error mes ...

Creating a custom component results in an extended duration to 'eliminate' its children

1I am currently facing an issue with a time-table component created using vue.js. It contains approximately 200 nested child timeline components, making it quite complex (I wanted to share an image but lacked the reputation to do so). The main problem lie ...

Exploring the functionality of scrollTo in React

I'm currently working on developing a carousel feature that should scroll when the user interacts with the navigation buttons. However, despite my efforts, I am facing an issue where the scrolling functionality is not working as expected in my code sn ...

Sending data to Layout for customizable routes (App Router) in Next.js version 13

Looking to pass props to the Layout component for dynamic routes in my project structure: /app -/(site) --/layout.tsx --/page.tsx --/[slug]/page.tsx --/[slug]/layout.tsx In the site layout: export default async function IndexRoute({ children }: { chil ...

Close overlay panel in PrimeFaces calendar

One of the components I’m currently working with is an overlayPanel that contains a calendar. Here's a snippet of the code: <p:overlayPanel hideEffect="fade" showCloseIcon="true" dismissable="true" > <h:form> <p:p ...

Tips on maintaining the persistence of realms, users, and roles in Keycloak while running it through docker and docker compose

My Spring Boot Java application relies on Keycloak for authentication and authorization. I've set up my realm, users, and roles in Keycloak and now I want to dockerize the application using Docker Compose. However, when I try to create a Keycloak inst ...

Ruby on Rails displaying landing page with variations between localhost and HTML file

I've been putting together a landing page for a website project and stumbled upon an amazing template online. After loading everything up and ensuring that the file links were set correctly, I decided to double click on the HTML file within my views f ...

Issue with three.js memory leak due to malfunctioning .dispose() method or incorrect usage

Greetings! I'm currently facing a memory handling issue. Despite researching various forums, I am still unable to identify the problem in my code. My project involves combining d3.js with three.js to create a visualization of nodes resembling planet ...

Java program to add elements of one array into another array at specified position

Consider the following list of names: String[] arrayOfName = {"Elephant", "Deer", "Fish", "Crocodile", "Cat", "Dog", "Bird", "Butterfly", "Chicken", "Ant", "Snake", "Lion", "Horse", "Wolf", "Panda", "Swan", "Lobster"}; I am contemplating creating a new a ...

Press the corresponding buttons (delete, view, edit) in a sequential order while utilizing selenium in C#

Can someone please assist me with how to click the action links in a table? Here is an example of the HTML structure: Lebron James | EDIT | VIEW | DELETE Chris Paul | EDIT | VIEW | DELETE Carmelo Anthony | EDIT | VIEW | DELETE The correspondi ...

Exploring deep levels of JSON data structures

I am currently dealing with nested JSON files and looking to extract specific data from them. Here is an example of the JSON file I'm working with: My goal is to identify all objects in the file where the value for "size" is equal to 1, and then add ...

Missing Ajax Functionality in Laravel Application

The code snippet below was created by me... <script> $('#spielAuswahl').on('change', function(e){ console.log(e); var spielID = e.target.value; //ajax $get.('/spieler?spielID=' + sp ...

Checking for element presence in selenium 2.0

Hello everyone, I am currently utilizing webdriver and I need to utilize the isElementPresent function from Selenium RC. To achieve this, I have to emulate Selenium RC by following these steps: import org.openqa.selenium.By; import org.openqa.selenium.Web ...

Issue with rendering React Three JS in the browser

I recently attempted to follow a tutorial at https://www.youtube.com/watch?v=wRmeFtRkF-8 However, upon running my code, all I see is a blank canvas with a black background (as specified in the CSS). Interestingly, I can tell that the canvas is functional ...

Utilizing dynamic variable invocation

When working with Angular Components, I need to utilize various variables. export class AppComponent{ value1; value2; value3; value4; print(position) { console.log(this['value'+position]); } } How can I implement this function ...