Selenium IDE Xpath vs Webdriver Xpath

locators in selenium webdriver java
xpath and css selectors in selenium
dynamic xpath in selenium webdriver
xpath locators in selenium
css selector in selenium webdriver
how to find xpath in chrome for selenium webdriver
xpath by id
text() in xpath

I'm newbie with Test Automation. When I locating element through Firepath with target:

 xpath=(//td[contains(@id, 'catProdTd_4723290')]/div/div[2]/h2)

Firefox founds that element and verify text. But, when I trying to locate this element with Visual Studio 2012 and Selenium Web driver, I constantly have error: "Unable to locate element: {"method":"xpath","selector":"//td[contains(@id, 'catProdTd_4723290')]/div/div[2]/h2"}" .

I tried escaping:

//td[@id=\"catProdTd_4723290\"]/div/div[2]/h2

but nothing. When I use isElementPresent method, it founds elements. Is there some special method or rule that should be use when writing Xpath for WebDriver ? I defined ISelenium variable, WebDriver... Clicks works, WaitForPageToLoad works, but this can not locate element.

IWebElement we= driver.FindElement(By.XPath("//td[contains(@id, 'catProdTd_4723290')]/div/div[2]/h2"));

HTML from page:

<td class="productItem" id="catProdTd_4723290"><div class="product-details">
    <div class="product-aside"> <img border="0" alt="Fork and Spoon Set" src="/_photos/store/glass-large.jpg" id="catlproduct_4723290">
     </div>
    <div class="product-main">
    <h2 class="product-name">Fork and Spoon Set</h2>
    <div class="price"><strong>$17.99</strong></div>
    <hr>

    <div class="attributes"></div>
    <hr>
    <div class="product-col-1">
    <div class="qty"> Quantity: <strong><input type="text" value="1" name="AddToCart_Amount" class="productTextInput" id="Units_4723290"></strong></div>
    <div class="stock">(N/A in-stock)</div>
    </div>
    <div class="product-col-2">
    <input type="submit" onclick="AddToCart(192951,4723290,'',4,'','',true);return false;" value="Buy Now" name="AddToCart_Submit" class="productSubmitInput">
    <div class="wish"><a href="/FavoriteProcess.aspx?OID=4723290&amp;OTYPE=27" class="favoritelink">Add to Wishlist</a></div>
    </div>
    <div class="product-description">
    <h4>Product Information:</h4>
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit.  Aenean
    commodo ligula eget dolor.  Aenean massa.  Cum sociis natoque penatibus
     </div>
    </div>
    <!-- End Main -->
    </div>
    <!-- End Product Details -->
</td>

I must add that I try to wait during debug and with

Manage().Timeouts().ImplicitlyWait

but nothing. This happens on other places also. I using Firefox for tests

You are running into dynamic attributes.

My first recommendation to you. Switch to CSS.

My second recommendation, instead of boiling down into an entire parent-child hierarchy, why don't you just KISS!

So, lets look at your issue. You are trying to fetch the product name. Easy.. we can use classes here.

css=td.productItem h2.product-name

voila, it was that easy to fetch.. instead of having this huge ugly xpath selector, we've simplified it to a css selector.

So onto the next problem, if we have multiple of td.productItem's on the page, we can use a couple things.

Try,

css=td.productItem:nth-child(1) h2.productName

That will select the first td with class, productItem.

note: you may need to specify the td's parent.. e.g. css=div#container td.productItem:nth-child(1)

More specifics...

The reason your xpath is failing, is because of that catProdTd_4723290 id assigned to the <td> element being generated automatically, rendering that element unselectable. You can work around that, by doing starts with. for example, with css -

css=td[id^='catProdTd']

will select that <td> take note though, that there might be more than 1 element selected.

Locators in Selenium IDE: CSS Selector, DOM, XPath, Link Text, ID, Locator is a command that tells Selenium IDE which GUI elements ( say Text Box, Locators in Selenium IDE: CSS Selector, DOM, XPath, Link Text, ID Inspect the "Email or Phone" text box using Firebug and take note of its ID. Creating Object Repository in Selenium WebDriver: XML & Properties file. Summary – Selenium IDE vs. RC vs. Webdriver. Now you would have a clear idea of the difference between Selenium IDE, RC, and Webdriver. Web driver is the latest and the greatest of them all, and it also has excellent support from its development community whereas the Selenium RC has limited support.

I suggest using such a method for waiting:

 public bool boolWaitForElementIsDisplayed(By locator, int secondsToWait)
    {

        WebDriverWait Wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(secondsToWait));

        try
        {
            var FoundElement = Wait.Until<Boolean>(d =>
            {
                try
                {
                    return (d.FindElement(locator).Displayed && d.FindElement(locator).Enabled);
                }
                catch
                {
                    return false;
                }
            });
        }
        catch (WebDriverTimeoutException)
        {
            return false;
        }
        return true;
    }

and then check as follows:

IWebElement h2Element = null;

string xpath = "//td[contains(@class,'productItem')]/div/div[contains(@class,'product-main')]/h2";
if (boolWaitForElementIsDisplayed(By.XPath(xpath), 30))
    h2Element = Driver.FindElement(xpath);

Selenium IDE Xpath Tutorials, If you have worked with selenium IDE then you knows that sometimes elements does not contains id or name. Locating element by xpath is the another way of  Stack Overflow Public questions and Selenium IDE Xpath vs Webdriver Xpath. when I trying to locate this element with Visual Studio 2012 and Selenium Web

So, the problem was that page isn't loaded. Why? Because WebElement.Click() not works. Why Click not working?! I don't know. I resolved problem with clicks using JavascriptExecutor:

IJavaScriptExecutor executor = (IJavaScriptExecutor)chauffeur;
IWebElement webel1 = chauffeur.FindElement(By.CssSelector("#nav ul:nth-child(1) li:nth-child(2) a[href='/products']")); 

Instead of using

webel1.Click(); 

which not works, I used:

executor.ExecuteScript("arguments[0].click();", webel1); 

XPath in Selenium WebDriver: Complete Tutorial, An introduction to the advantages of using XPath with your Selenium web testing scripts. If you just use Selenium IDE, it finds an element on a web page by its ID​. In addition, XPath may be used to compute values (e.g., strings, numbers, or Boolean An Introduction to XPath in Selenium WebDriver. XPath in Selenium WebDriver is used to find an element on the web page. There are two types of XPath: 1) Absolute & 2) Relative. we will learn Xpath methods Contains(), Using OR & AND, Start-with function, Text(), XPath axes, Following, Ancestor, Child, Preceding, Following-sibling, Parent, Self, Descendant.

I want to find the xpath for the image, If you want to use XPath to locate elements with the Selenium IDE you have Type into the search field: “Selenium” and hit enter or click on the search button. Selenium IDE- Locating Strategies By XPath with Introduction, features, selenium basic terminology, what is selenium, selenium limitations, selenium vs qtp, tool suite, selenium ide, ide-installation, ide-features, ide-first test case, ide-commands, ide-creating test cases manually, ide-login test etc.

What is the difference between cssSelector & Xpath and which is , This article on XPath in Selenium will help you understand A User's Perspective On Selenium IDE · QTP vs Selenium: Know the It is designed to allow the navigation of XML documents, with the purpose of selecting individual elements, attributes, or some WebDriver driver = new ChromeDriver();. Selenium Xpath Tutorials, Xpath Checker, Selenium IDE Xpath Tuesday, 02 June 2020 Selenium WebDriver Java Framework Course Limited Time Offer for $20

XPath for Selenium IDE, XPath can be used as a substitute when you don't have a suitable id or name attribute for the element you want to locate. XPath provides locating strategies like:. When one extracts the xpath of the object, one can use that object using Selenium IDE, Selenium RC, or Selenium Web-driver. Xpaths are constants. They do rarely change. If there is a text-box, and the label of the text-box changes but the position does not change then xpath remains the same. Sometimes xpaths alo vary everytime you refresh the page.

Comments
  • Does that element become visible if something happens on the page? Does it take time to appear? What browser are you running against? Any IFrames? Please post the section of HTML that you are running it against.
  • This is a fantastic answer. @garvan, if you want more info on selectors, the PDFs on this page have helped me greatly: simple-talk.com/dotnet/.net-framework/…
  • I tried CSS selectors (although I thought to work only with xpath) error: "Unable to locate element... " continues. I tried to find elements on other site, that has no dynamic generated elements, and "Unable to locate element..." again. This driving me crazy, in Firepath location is found, either by xpath or css, but when go through Visual Studio and WebDriver "FindElement" can not locate it ?!