I having error locating an element using Xpath

locators in selenium webdriver java
selenium find element by xpath
css locators in selenium
selenium locators best practices
what are the different types of locators in selenium? which one is preferable to use?
xpath using class
locators in selenium tutorials point
how to locate elements in selenium

Having the below html. I want to select a particular element using xpath

    <tr role="row" id="gridview-1034-record-ext-record-1" data-boundview="gridview-1034" data-recordid="ext-record-1" data-recordindex="0" class="x-grid-row x-grid-data-row x-grid-row-selected x-grid-row-focused" tabindex="-1">
        <td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1010 x-grid-cell-first x-unselectable " id="ext-gen1139">
        <div unselectable="on" class="x-grid-cell-inner " style="text-align:left;">03 Oct 2018</div>
        <td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1013 x-unselectable " id="ext-gen1140">
        <div unselectable="on" class="x-grid-cell-inner " style="text-align:left;">Sales Quotation</div>
        <td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1016 x-unselectable " id="ext-gen1141">
        <div unselectable="on" class="x-grid-cell-inner " style="text-align:left;">000079</div>
        </td><td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1019 x-unselectable " id="ext-gen1142">
        <div unselectable="on" class="x-grid-cell-inner " style="text-align:left;">ABC Bearings Ltd.</div>
        </td><td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1022 x-unselectable " id="ext-gen1143">
        <div unselectable="on" class="x-grid-cell-inner " style="text-align:left;">&nbsp;</div></td><td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1025 x-unselectable " id="ext-gen1144"><div unselectable="on" class="x-grid-cell-inner " style="text-align:right;">8,000.00</div>
        </td><td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1028 x-unselectable " id="ext-gen1145">
        <div unselectable="on" class="x-grid-cell-inner " style="text-align:left;">No</div></td>
        <td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1031 x-grid-cell-last x-unselectable " id="ext-gen1146">
        <div unselectable="on" class="x-grid-cell-inner " style="text-align:left;">Approved</div>

I am unable to locate the below element and click it using XPath using the below code.

Help would be much appreciated


You can use contains(text(),'...') for matching the div you want and then click it.

element = driver.findElement(By.xpath("//tbody/tr/td/div[contains(text(), 'div text you want')]"));

is the unique identifier for the element and it does not change. This makes 'Ids' a very explicit and reliable way to locate elements on the page. Also, all browsers also have highly efficient methods to get an object on the page using their ids. Hey Angad, for locating elements based on text, you can use the text() method. Following expression will get elements that have text nodes that equal ‘Apple': xPath usage = //*[text()='Apple']

I agree with answer from @Faisal Maqbool. I would shorten the xpath a bit as follows,

element = driver.findElement(By.xpath("//div[contains(text(), 'div text you want')]")); element.click();

You want to try and select the exact element directly if you can, complicate the xpath only if you can find no other option.

The HTML tag you are trying to match has a class attribute and a data-url attribute but not an href attribute. Either you mistyped the HTML tag in your question or  The success rate of finding an element using Xpath is too high. Along with the previous statement, Xpath can find relatively all the elements on a web page. Thus, Xpaths can be used to locate elements having no id, class or name. Creating a valid Xpath is a tricky and complex process.

Learn about different Locators in Selenium Webdriver - ID, XPath, Name, DOM, in our guide to getting started with the Selenium framework with Python. No matching elements result in a NoSuchElementException error. You want to try and select the exact element directly if you can, complicate the xpath only if you can find no other option. share | improve this answer answered Oct 4 '18 at 6:15

Element with text '000079' is present on tr[1]td[3]. You can use the following solution:

 WebElement e1= driver.findElement(By.xpath("//tbody/tr[1]/td[3]/div[1]"));
    String Str=e1..getText();
    { System.out.println("Text matched"+Str);
    System.out.println("Text Mismatched"+Str);

Here, we will learn how to locate web elements using locators in ID; Name; Linktext; Partial Linktext; Tag Name; Class Name; DOM Locator; CSS Selector; Xpath ch-error-innertxt"></span> </div> </div>The syntax for locating via They may or may not be unique on a page, having multiple elements. In  If a simple XPath is not able to find a complicated web element for our test script, we need to use the functions from XPath 1.0 library. With the combination of these functions, we can create more specific XPath. Let's discuss a 3 such functions – By using 'contains' function in XPath, we can extract all the elements which matches a

String Str = "000079";
            List<WebElement> listEle = driver.findElements(By.Xpath("Your Xpath"));
            System.out.println("List Printer " + listEle);
            Iterator<WebElement> iter = listEle.iterator();
            System.out.println("Iterator Print" + iter);
            while (iter.hasNext()) {
                WebElement we = iter.next();
                System.out.println("Web Element We " + we.getText());
                if (we.getText().equals(str)) {

If the argument is omitted, then a runtime error is reported if the context item is cannot do this, because there is nothing to stop a key value containing a space. XPath 2.0 provides two ways to locate elements in a different document. The biggest disadvantage of using this as locating an element is, if during the course of development any changes made in the path, may lead to a failed XPath expression.

XPath locator examples This page contains some examples to get you started. After that, I recommend the XPath XPath can find an element by ID like this: Locating elements based on class. In order to locate an element based on associated class in XPath you must consider that the element could have multiple classes and defined in any order. However with CSS locators this is much simpler (and faster). XPath: //div[contains(@class, 'article-heading')] CSS: css=div.article-heading

Use the XPath syntax to select elements on this web page You can open an XML document in any text editor and the data it contains will See if you can locate its various elements, and this challenge box in particular. For now, don't worry too much about error messages if you see any in the console when you open it. A raw XPath traverses the hierarchy from the root element of the document (page) to the desired element: /html/body/p/a . Child of Element ID. XPath can find an element by ID like this: //*[@id="element_id"] So if you need to find an element that is near another element with an ID, like the link in this example:

Using CSS Selector as a Locator Selenium tutorial #6 - In our previous Like Xpath, CSS selector can also locate web elements having no ID,  In the previous post we have seen “locators in Selenium“. In this post, we discuss “How To Locate Element By XPath Locator”. Find the below links on How to find elements on a web page using different types of locators. 1. “How To Locate Element By ID Locator” 2. “How To Locate Element By Name Locator” 3.

  • please provide more information as mock html, error, goal.
  • Your html example starts with tr, but your xpath starts with tbody ... please complete your example.
  • There is <tbody> </tbody> and i am unable to click the row i also tried this element(by.id('gridview-1034-record-ext-record-2')).click();
  • stackoverflow.com/users/3021395/powerstat ??
  • Your problem is that the xpath normally would result in multiple elements as result - so in your case the first one might be picket but not the one you want. You need to be more precise in your xpath like with adding a [3] for the third node to pick.
  • have you tried element = driver.findElement(By.xpath("//div[contains(text(), 'div text you want')]")); this ?
  • can explain little more how to use your solutino
  • Checkout the updated answer and let me know the status
  • element.all(by.cssContainingText('[class*="x-grid-row x-grid-row-alt x-grid-data-row x-grid-row-selected x-grid-row-focused x-grid-row-over"]','')).click(); i did tried this too but it did not worked
  • You asked about XPath, so my answer is based on XPath, update the question with the error you see with my answer
  • i have used many alternative to select that row but i found none to be worked for me plz help...
  • var e1= element(By.xpath("//tbody/tr[1]/td[3]/div[1]")); var Str=e1.getText(); if(Str="000079") {e1.click(); } else { System.out.println("Text Mismatched"+Str); }
  • it says element is not intractable