What is the best way to locate an element using xpath?

I've been on the hunt for a "button" to click, but unfortunately it doesn't have an id attached to it. No matter what I try - Xpath or cssSelector - nothing seems to work.

Here are the elements of this elusive button:

div style="cursor: grabbing;" title="Approved Forms" onclick="goToForms(0)" class="col-lg-2 col-sm-6"

I attempted the following methods:

driver.findElement(By.xpath("//div[contains(@title,'Approved Forms')]")).click();
driver.findElement(By.xpath("/html/body/div[3]/div/div/div[2]/div[1]")).click(); *copy xpath*
driver.findElement(By.cssSelector(['title="Approved Forms"]')).click(); *encountered syntax errors and unsure about correct implementation*

Answer №1

To find the specific element, you have the option to utilize one of the following Locator Strategies:

  • cssSelector:

    WebElement element = driver.findElement(By.cssSelector("div[title='Approved Forms'][onclick^='goToForms']"));
    
  • xpath:

    WebElement element = driver.findElement(By.xpath("//div[@title='Approved Forms' and starts-with(@onclick, 'goToForms')]"));
    

It is recommended to implement a WebDriverWait for the method visibilityOfElementLocated(). You can apply one of these Locator Strategies:

  • cssSelector:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[title='Approved Forms'][onclick^='goToForms']")));
    
  • xpath:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@title='Approved Forms' and starts-with(@onclick, 'goToForms')]")));
    

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

Is there a way to locate the content enclosed within a span tag without any specific class or ID attribute using Selenium with Python?

I have been attempting to utilize Selenium in order to locate the text within a span element that lacks any specific attributes such as class or id for identification. Here is the HTML structure: HTML snippet obtained from inspecting the element in Chrome ...

Error occurred in the main thread: org.openqa.selenium.InvalidElementStateException:

Today, I encountered an issue while trying to send input string to the search text box on a Chrome browser: // attempting to search for a text in Google WebDriverWait wait3 = new WebDriverWait(driver1,30); WebElement textBox = wait3.until(ExpectedConditio ...

Determining the Specific Element for Sending Keys in Selenium: A Guide

I constantly encounter the same issue while using Selenium with Python - I frequently struggle to locate the element to perform send_keys() on. For instance, when trying to search for an item on the eBay homepage. Every time, I find myself experimenting w ...

Error encountered while running Selenium: "main" thread exception - org.openqa.selenium.remote.UnreachableBrowserException

I'm currently using Selenium with Firefox webdriver and encountering an exception when trying to create my webdriver instance. WebDriver driver; driver = new FirefoxDriver(); The strange thing is that the code was working fine before, but now it&apo ...

Is it feasible in Selenium to report a failure for a particular test case step and carry on with the rest of the steps if necessary?

Is it possible to report and continue with remaining steps in Selenium if a step fails for a test case? The current behavior halts the execution if there is an exception. Below is an example of how a test case looks like: public class TC002_abc extends Op ...

Setting a timeout for the @BeforeSuite method in TestNG using a Listener class

I need to establish a timeout for a BeforeSuite method using a Listener class in Selenium WebDriver. I am struggling to find resources on how to do this effectively, especially since I have multiple test suites with timeout settings for each individual tes ...

python selenium perform a click action at the present cursor location

I am dealing with a dropdown menu that requires clicking to activate. Unfortunately, the elements of the dropdown are not visible in the html code, making it impossible for me to locate and click on them directly. To navigate the dropdown, I use arrow keys ...

Selenium - How to pass a file path to a dynamically generated input element that is not visible in the DOM

Check out this example of HTML code: This is how you create a visible button and display the selected file: <button id="visible-btn">visible button</button> <p>selected file is: <span id="selected-file"></spa ...

Navigating my path with precision using XPath

Currently, I am utilizing webdriver.find_element_by_link_text('searchterm') in Selenium to retrieve a webElement. Once obtained, I extract the value of href by using webElement.get_attribute('href'). Subsequently, I am able to pinpoint ...

The underlying technical rationale for configuring the zoom level to 100% for the IE Driver

Is there a specific reason why IE Driver has to be set at 100% zoom level from a technical standpoint? ...

Is it possible to execute Visual Studio classes (unit tests) directly from an Excel spreadsheet?

Just a quick question: Is it possible to click a button in Excel that triggers a specific C# unit test in Visual Studio, while also sending and receiving data from Excel? I have experience with running test cases in HP QTP/UFT, but now I am using Selenium ...

Retrieve various JSON entities using Java

Currently, I am facing an issue while trying to retrieve data from Json in Java code. Even though I have written the necessary java code for this task, I am only able to access the "title" field with a value of Event 1. Additionally, when attempting to acc ...

What methods are available for transforming my Python GUI program into a desktop application?

Recently, I created a Python program with a GUI included. Now, I'm looking to turn this program into a desktop app that can be opened without the need for a text editor or command line execution. The program itself searches for and plays the first vid ...

Struggling to sequentially open multiple websites using Selenium?

I'm currently working on automating a task that involves opening and closing a list of websites one by one. However, I've run into an issue where the program successfully opens and closes the first website, but fails to open the subsequent ones i ...

Having trouble sending information to a servlet with jQuery

As I work on developing an application utilizing jquery in conjunction with servlets, I have integrated the jquery theme roller for enhancing the interface on my Login.jsp page. <script> $(document).ready(function() { $("#dialog").dialog(); }); ...

Issue Encountered: Unable to locate the class file for org.openqa.selenium.internal.Locatable

I am currently facing a build error in Eclipse while using Selenium and Maven to develop a project. The error message states: Cannot find the class file for org.openqa.selenium.internal.Locatable. Upon closer inspection, I realized that the version of Sel ...

Select all elements in a column using Selenium in C# when two specific conditions are satisfied (Multiple Conditions)

My task is to extract all elements from the td of an HTML table. However, when I attempt to locate elements by xpath with multiple conditions, it fails and retrieves 0 elements. I have no issues retrieving all values with a single condition, indicating tha ...

I encountered a selenium chrome webdriver error: An exception was thrown in the main thread, displaying the message "org.openqa.selenium.SessionNotCreatedException: Unable to initiate a new remote session

I encountered an error while trying to run Selenium on Chrome, it said: Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Unable to create new remote session. Any advice on how to resolve this issue? The full error message is provi ...

What is causing find_by_css to return nothing when using nth-child?

Currently, I am facing an issue when trying to extract the "href" link from the following HTML code: https://i.stack.imgur.com/Gtzf4.png This is the code that I'm using: from selenium import webdriver from splinter import Browser from bs4 import Be ...

"Troubleshoot: Resolving Issues with Opening URLs in Selenium from a

I am encountering an issue while trying to open URLs from a TXT file using the Selenium WebDriver. The code I am using is written in Python 3.4.3. Could you help me identify the problem in this code? from selenium import webdriver with ope ...