Is there a way to simulate a middle mouse click using Selenium and JavaScript?

I am looking to simulate a middle button mouse click on a text URL within a webpage using Selenium code in JavaScript so that it opens in a new tab.

Here's an example:

await driver.get("https://google.com");

// This is the xpath for the 'terms' text URL
await driver.findElement(By.xpath('/html/body/div[1]/div[5]/div[2]/div[2]/a[2]')).click();

Answer №1

Success! I have discovered a resolution.

All that is needed is to transmit two keys simultaneously: ctrl & enter

 await driver.get("https://google.com");
 //this is the xpath for the 'terms' text URL
 await driver.findElement(By.xpath('/html/body/div[1]/div[5]/div[2]/div[2]/a[2]')).sendKeys(Key.CONTROL, Key.ENTER);

Answer №2

Check out this code snippet that may not provide the direct solution, but could lead you to the ultimate answer. Explore more on (How to detect middle mouse button click?)[this resource]

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

Using async/await in an Express route handler

Currently, I am facing an issue with my node/express route. The route collects information through url parameters and then uses these parameters to trigger a separate function that takes some time to execute. Despite using async/await to pause the executio ...

Tips for utilizing one catch block to handle both TestNG assertion errors and RunTime exceptions

In my TestNG framework with Java and Selenium, I have created two custom methods to handle WebElement interactions. Here they are: // Checks if a WebElement is present on the screen public boolean isElementPresent(WebElement element) { try { e ...

Get and show the response from a jQuery GET call

I'm currently working on fetching and displaying data using the ESV API available at Interestingly, the code seems to be functioning properly within the codecademy.com domain but is encountering issues on the esvapi.org domain. Here's a lin ...

What is the best way to save the toggleClass value of various selectors along with their corresponding classes in localStorage?

I've been attempting to preserve the toggled class of multiple elements using local storage or cookies in order to maintain the toggled class after a page refresh. Despite trying various solutions such as js cookie and other tutorials, I've had n ...

JQuery Ajax call fails to retrieve any information

I have been experimenting with JQuery Ajax methods. I created a basic Ajax request to retrieve specific 'tagged' photos from Flickr. Here is the code snippet I am using: function initiateSearch() { $(function() { var tagValue ...

Ways to convert a jQuery object into HTML that can be utilized?

When running the code below, an alert message of "object Object" is displayed: var shipImgs = $("#div").children(); alert(shipImgs); The container with id "div" includes a total of 4 children (image tags). <div id="div"> <img src="/imgs/spa ...

Hide Address with Jquery Dialog Box

How can I securely redirect a user to another page within the same website? In my specific situation, I have a form where users input information. For cancellations, they should have options available. When a user clicks the cancellation button, a dialog ...

Getting the WebElement object by manually clicking an element while in an active WebDriver Session

I am currently developing a Java Swing application for managing object repositories in Selenium scripts. This application will launch a WebDriver instance and allow users to manually navigate to the desired element for inspection. My goal is to capture th ...

Receiving the error message 'Cannot find symbol Test' in Java using Selenium with TestNG

I am experiencing an issue with my TestNG setup where I am unable to run a test. The error message I am encountering can be seen here. After checking my POM.xml file for errors and reviewing the code in my test page, such as: import Pages.SearchPage; ...

Acquiring specific strings from an element retrieved by Selenium using Python

My project involves using selenium in python to scrape a website and extract elements with specific classes 'left' and 'right'. The selenium method returns an object that, when printed iteratively, displays a series of text. left = dri ...

Implementing the Delete feature using AJAX in this specific scenario: What is the best approach?

My website utilizes PHP, Smarty, MySQL, jQuery, and other technologies. It involves fetching a large amount of data from the database and presenting matching question ids on a webpage. This process of retrieving and displaying the matching question ids for ...

Issues with Selenium Webdriver in Firefox

When using the Selenium plugin on Firefox, an error message stating "[error] current test case not found" always appears. Has anyone figured out how to resolve this issue? ...

Replicate and customize identical object for creating instances in JavaScript

I am working with an object that has the following structure: var customObject = function() { this.property = "value"; }; customObject.prototype = new otherObject(); customObject.prototype.property2 = function() {}; This is just a snippet as the ac ...

Utilize WebdriverIO to loop through elements and trigger a click event on a link

I am currently using Javascript and webdriverio (v2.1.2) for data extraction on an internal website. The process involves: Authentication Opening the necessary URL once authenticated Searching for an anchor tag with a specific keyword on the new page Cli ...

How is the same-domain policy applied to popup windows that have JavaScript enabled in the URL?

Is it possible to achieve something similar to this? var w = window.open("javascript: makeAnAjaxRequest();"); I'm curious whether the Ajax request, which is triggered after the new window is opened, would be considered a cross-site request. Does the ...

Experiencing a Typescript issue while trying to set a string as the state of a React component with a specified TS type

I've defined a state in my React component for a specific data type called Color. \\ state const [messageSeverity, setMessageSeverity] = useState<Color>('success'); \\ TS type export type Color = 'success&ap ...

javascript function not being invoked

Currently, I have incorporated the following HTML <html> <head> <Title>EBAY Search</title> </head> <script language="JavaScript" src="ajaxlib.js"></script> <body> Click here & ...

What is the best way to retrieve multiple image file names using JavaScript?

I attempted to use the code snippet below to retrieve the names of all the images I selected, however when I used 'alert', I only received one name even though I selected multiple. My goal is to have all the names of the selected photos saved in ...

Autocomplete feature in Angular not showing search results

I am currently using ng-prime's <p-autocomplete> to display values by searching in the back-end. Below is the HTML code I have implemented: <p-autoComplete [(ngModel)]="agent" [suggestions]="filteredAgents" name="agents" (completeMethod)="f ...

Discovering elements that feature specific text is a breeze when utilizing XPath in conjunction with Selenium and Python

Having experimented with this code snippet: driver.find_elements_by_xpath("//*[contains(text()),'panel')]") I noticed that it is only returning one result instead of the expected 25. My goal is to locate all xpath id's on a webpage in sel ...