There are 4 nodes in Locators that match - what is the best way to create an XPath in Selenium for handling 4 matching elements?

Here is my code snippet using Selenium:

driver.findElement(By.xpath("//a[@href='/agent-create-profile'][1]")).click();

An error occurred: Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@href='/agent-create-profile'][1]"}

The Selectors Hub displays 4 matching elements based on the provided xpath:


<a tabindex="0" class="primary-button__PrimaryButton-iSIsNJ cjTSCX abstract-button abstract-button--size-large" href="/agent-create-profile" style="" xpath="2"> Create Profile </a>

<a tabindex="0" class="primary-button__PrimaryButton-iSIsNJ cjTSCX abstract-button abstract-button--size-large" href="/agent-create-profile" style="" xpath="3"> Let's start </a>

<a tabindex="0" class="primary-button__PrimaryButton-iSIsNJ cjTSCX abstract-button abstract-button--size-large" href="/agent-create-profile" style="" xpath="4"> Agent Sign up </a>

<a rel="noopener noreferrer" class="navigation-section__menu-link" href="/agent-create-profile" style="" xpath="4"></a>

Answer №1

It appears that there is a missing pair of parentheses in your xpath query, specifically at the beginning and before [1]

Consider using the following revised xpath:

driver.findElement(By.xpath("(//a[@href='/agent-create-profile'])[1]")).click()

If you prefer an alternative approach to target the first element:

driver.findElement(By.xpath("//a[contains(text(),'Create Profile')]")).click()

Answer №2

Since xpath attributes are distinct: there is an alternative approach for utilizing xpath in addition to the current solution:

driver.findElement(By.xpath("//a[@xpath='1']"));

Similarly, css functions in a very comparable manner:

driver.findElement(By.cssSelector("a[xpath='1']"));

Answer №3

Consider using the partialLinkText method when attempting this:

 driver.findElement(By.partialLinkText("Create Profile")).click();

Alternatively, you could gather all links as a list and iterate through them in a for loop to find the desired link text using the getText() method. If possible, please provide the website link for reference.

List<WebElement> linkList = driver.findElements(By.xpath(“”));
for(WebElement link : linkList) {
   if (link.getText().contains(“create-profile”)) {
   link.click();
   break;
   }
}

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

Steps for differentiating parameters with identical names in JSON

I've come across numerous questions regarding String to JsonObject conversion, but I'm struggling to find a working solution. I'm currently using Java 17 and trying to retrieve data from an API where two values need to be split into separate ...

Repositioning divs using HTML, CSS, and PHP

My divs are moving around when I resize the window. Can someone assist with this issue? I have HTML/PHP code for login and page retrieval. This is my HTML body section with PHP for logging in and managing the webpage content. <div class="menu"> ...

Problem with the menu button overflowing

I'm exploring the process of designing burger menus for the mobile version of my site. I've successfully created a basic burger menu with some JavaScript assistance, but I encountered an overflow issue. The burger menu functions properly, but the ...

Using Java to manipulate an ArrayList of strings without incurring any data charges

I am working on creating an ArrayList with a Class named "Company" to store a list of companies retrieved from Firestore. The code for the class is as follows: public ArrayList<String> companyList (View view) { CompanyList.add(0, "Sele ...

Performing a specific x y coordinate click with Python Selenium

After searching through numerous old questions, I still haven't found a solution to my problem. I need to be able to click on a specific x and y position without relying on DOMs or XPATHs. Just the coordinates themselves. Can anyone help me with this? ...

A guide to modifying the color of the Menu component in material-ui

Currently, I am utilizing the Menu component from material-ui and facing an issue while trying to modify the background color. The problem arises when I attempt to apply a color to the Menu, as it ends up altering the entire page's background once the ...

Arrays in Javascript (correlating)

I'm having trouble figuring out what needs to be included in this code (I apologize that it's in German, it's an array corresponding to images for JavaScript.) function namenZuBildern(){ var bilder = new Array( "000227", "000228", " ...

Element not found: org.openqa.selenium.NoSuchElementException

I'm currently developing a script for mobile number verification using One-time password (OTP) functionality. However, I encountered an issue while the OTP popup is displayed as I am unable to input any value into the text field. The system keeps thro ...

Selenium "Confirm your identity as a human" Message Box on Firefox

Has anyone discovered a solution for dealing with the occasional "Please Verify you are human" popup in FireFox while using Selenium and BeautifulSoup? It seems to appear every 500 or 1,000 URL requests, so I'm hoping for an automated workaround. Cur ...

Different Android Implementations Experience Varying Success Rates with Triggering Events

Within my code, I've utilized: public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback, SensorEventListener To handle touch events and accelerometer events. By using @Override, I successfully managed to override the tou ...

A guide on creating an object from a class within a Swing GUI Java file

I am presenting the code of my HomePage.java file, where I have instantiated the LoginPageService class object to utilize its methods. public class HomePage extends javax.swing.JFrame { CardLayout cl; private LoginPageService service; /** ...

A button featuring an extensive label that utilizes text-overflow ellipsis is initially set with a max-width of 700px. However, it should dynamically shrink when the viewport size decreases

My Request I need a button that can accommodate both long and short labels, with the text getting cut off at 700px using ellipses. The button should adjust to the available space when the viewport is smaller than 700px. The length of the label can vary gr ...

Is there a way to streamline the jQuery code provided below?

I have programmed 6 buttons and successfully implemented jQuery to display a block of data upon clicking each button. Is there a way to condense the jQuery code? If so, what is the best approach to achieve this? $("#button1").click(function() { $("#te ...

How to attach onclick events to newly created div elements using JavaScript

Having some trouble adding click events to multiple dynamically created div elements. After creating divs with ids a0 through an, I want to assign them click events using a for loop. The problem is that when the click event occurs, I can't identify w ...

Could not find the iframe using Selenium Webdriver in Java

I am looking to select an element within an iframe that is located inside a popup window. While I can navigate inside the popup window, I am having trouble locating the iframe itself. The HTML code for the popup window is as follows: <html> <head ...

An example of streamlined performance within the most basic of loop structures

Consider a scenario where a loop contains an integer variable called counter that needs to be reset to 0 during each iteration. Which of the following two versions is more optimal? Version-1: for (int i = 0; i < 10; i++) { int counter = 0; ...

Error caused by @Autowired with NullPointerException in the Main class

My plan is to create a custom @Service that provides me with a Selenium WebDriver using dependency injection. Check out the code snippet below: import java.io.File; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.Chr ...

Copy and paste the hyperlinked text in Python 2.7

Currently, I am working with Python 2.7, Webdriver, and Chrome. When manually swiping the mouse across text that includes a hyperlink on a webpage, I am able to copy it to the clipboard. However, I need to find a way to automate this process. Locating th ...

Adjust the size of the image captured by the camera to match the dimensions of

I'm currently working on creating an application that showcases the photo right after it's captured. However, I've been facing difficulties in proportionately scaling the bitmap to fit the screen. I've experimented with imageView.setSc ...

Prevent overlapping of divs

Having some trouble with my portfolio page. I've got a section set up with nested divs, but when I try to adjust the position of the divs, they end up on top of each other. Can anyone offer some guidance? #portfolio { width: 650px; background-col ...