- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Unable to locate an element using xpath error in selenium-java
We may encounter the error - unable to locate element while working with Selenium webdriver. This leads to NoSuchElementException. This type of exception is thrown when there is no element on the page which matches with the locator value.
If error is encountered, we can fix it by the following ways −
Check if there is any syntax error in our xpath expression.
Add additional expected wait conditions for the element.
Use an alternative xpath expression.
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class XpathError{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //launch URL driver.get("https://www.tutorialspoint.com/index.htm"); WebElement m = driver.findElement(By.xpath("//*[text()='Library']")); m.click(); //explicit wait condition - visibilityOfElementLocated w.until(ExpectedConditions.visibilityOfElementLocated (By.linkText("Subscribe to Premium"))); System.out.println("Page title: " + driver.getTitle()); driver.quit(); } }
Output
- Related Articles
- How to find an element using the “XPath” in Selenium?
- How to identify nth element using xpath in Selenium with python?
- How to select element using XPATH syntax on Selenium for Python?
- Find Element and FindElements by XPath in Selenium
- Which exception is raised when an element is not found in an HTML DOM using XPath in Selenium?
- How to locate element by partial id match in Selenium?
- Clicking on elements within an SVG using XPath (Selenium WebDriver).
- The xpath of the element keeps changing, how do I find dynamic xpath for this element in Selenium
- Using the Selenium WebDriver - Unable to launch chrome browser on Mac
- What is xpath in Selenium?
- How to identify the nth sub element using xpath?
- How to use relative xpath for locating a web-element by particular Attribute in Selenium?
- How to find an element using the attribute “id” in Selenium?
- How to find an element using the attribute “name” in Selenium?
- How to find an element using the “CSS Selector” in Selenium?

Advertisements