Hot answers tagged

17

Yes. WebDriver supports using an XPath to locate an element by the text it contains. For example, the following XPath locates any div containing the string "Hello Justin": //div[contains(.,'Hello Justin')]


10

If you want to do it manually, then you will have to trace the path from the root of the host up to where the element is being generated. OR There is an easy way to get the xpath. In Mozilla FireFox install Firebug addon. Then install Firepath addon. Now visit your web page. Press F12 to open Firebug console. Click on the Firepath tab. Inspect the element ...


8

css=a[text='Log Out'] or a[innertext='Log Out'] Can you please try this one out? Or if that doesn't work and you still don't want to use xpath because it's slow, you can always try: link=Log Out. That's still better then xpath. EDIT: So i found a possible solution for you mate. If you are trying to find an exact String you could always use Regular ...


8

Don't forget that not only performance is better with CSS locators, it's the compatibility too that matters. We are testing on a multi browser environment in which we use: IE, SAFARI, FIREFOX, CHROME. On IE the xpath almost never works OR it is SO slow that it can't be managed. So we use CSS where ever we can. Unfortunately IE does not support many CSS ...


7

Well, in fact I am using xpath. The best way is to put a static (of course unique) id to the elements you want to refer.


7

Bruce, welcome to SQA. The label in the element in question is AgendaShowCapacity (1 remaining), but the label in your XPath is AgendaShowCapacity (1 remaining). I am not sure a blank and an   are equivalent in an XPath. Have you tried replacing that first blank with an   in your XPath? Another way to rule out the   ...


4

Sadly, it sounds like using the ID is not an option in this scenario A lot of websites implement security to prevent automating. While this makes your job more difficult it does improve the security of the application. So what can be done to bypass these security tricks? Use a different unique constant for that element This could mean using the class, ...


4

The Java Webdriver API has two methods for locating elements: findElement and findElements. findElement returns a single element matching the specified criteria (and otherwise throws an exception). findElements returns a list of matching elements, or otherwise an empty list. With the Java API, you would solve the problem by calling findElements and then ...


3

I think that your page (or part of it) is reloaded after performing listName.click(). After reload there is also a table which seems to be same as that one from before reloading but it is not the same one. You can count rows/columns in the table and use iterators in your loops. You will need to repeat WebElement table = findElementById("tableSection:...


3

As much as possible, please avoid using absolute xpaths, any small change in the page layouts and your xpaths may become completely useless, as much as possible, use the class names, ids etc... For the gmail sign-in, //div[@class='signin-box']//div[@class='email-div']//input[@id='Email'] is the xpath for the UserName Text field, likewise for the ...


3

This is the problem with Selenium IDE or any other recorder for that matter. It doesn't deal with dynamic data very well. If you really want to solve this problem without switching tools, you can try to use an xpath with an static anchor that's higher up in the DOM. For instance, you just start at //*[@id='edit_1_undefined'] and lets assume this element is ...


3

If there is an element above the button that is uniquely identifiable you could search only in that scope, assuming that the button is the only button in that scope. You could also try to partially match the button id (not sure if the entire thing changes, or just part of it) or match the button's class if it does not change. If there is no way to identify ...


3

XPath list indexes are one-based, not zero-based. Try li[1].


3

This is a nice place for a few CSS selectors. http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/ Thought it might be useful for people following this thread.


3

Did you try using regular expression or a substring in the xpath? Something like this may help: xpath = "//img[contains(@id,'x-auto')]" OR WebElement Estimates=webdriven.findElement(By.xpath("//img[contains(@id,'x-auto')]")); You can also use another xpath method starts-with(@id,'x-auto')


3

You can find an Xpath specification here. Your first xpath matches an anchor that contains exactly the string "test link". The last xpath matches an anchor whose contents includes the substring "test link". No doubt it is slower, but the depending on the circumstances, the performance difference may be insignificant. As far as I can tell, the first and ...


3

No jQuery will not be faster. IE has a very slow JavaScript engine compared to other modern browsers. Using jQuery selectors means you are using JavaScript to query the DOM, so you are instantly limited by IE's JavaScript engine. XPath support in IE is also via a JavaScript library (Google's wicked good xpath library), so XPath's will also be slow. The ...


3

you have to install firebug + firepath in mozilla firefox it will also provide you generated xpath You can also write the created xpath in the text field and press enter and it will validate the xpath .//*[contains(@placeholder,'Enter your email')] for creating xpath and locator search on google about locator strategies in selenium


3

//h4[contains(text(),'Some regular text ')] You are starting correct, though the first part, //h4[contains(text(),'Some regular text ')], looks for any h4 at any depth, with among its immediate children a text node containing Some regular text. Meaning, if that text appears after the strong element, it will also match the h4. /descendant::strong[...


3

You haven't explained the requirement very clearly, but perhaps the answer is //div[h3='Admin' and h4='Group'] Note that you shouldn't use contains() to test whether a node contains some string, unless you specifically want to allow it as a substring: you should use the '=' operator. And you should avoid using text() to fetch the text nodes: this would ...


3

If your edit class is unique on the page, then you can do .edit If you need to be a bit more specific, you could do more along the lines of .active.editing input.edit The point is, theres no "right" answer here, there are often many different css selectors you can build that can point to the same element. You want to find the balance between being short, ...


3

As the other answer already said, the preferred order is this: ID Locator NAME Locator CSS Locator XPATH Locator However, the reason why is simple: an ID is supposed to be unique, so once you have a certain element with a specific identifier, this is unlikely to change. Also, no matter where the element is moved to on a page, the ID stays the same. ...


3

My main criteria is readability and maintainability. Speed has never been as issue for me. There's other parts of the test frameworks I use that have much more significant speed issues. The basic idea is always 'what will it take to uniquely identify the element' with two principles: Don't over specify the page structure - this will make the selector ...


3

May be because there are many elements on the page with same xpath. check if you have more than one input type text element present on the screen. install plugin firepath on mozilla then paste your xpath over there it will highlight the elements ,if it highlights more than one element then you need to change the xpath


2

An example of something you can only do in XPATH is go the parent of the current node. So while I recommend using CSS when you can, sometimes XPATH is the only way. Edit : Actually, brain-fart on my side. The following site has two very useful charts that compare CSS and XPATH locators if those exist plus DOM locators for good measure, all with special ...


2

Have managed to resolve by using Selenium 2.0 as apposed to 1.0, apparently some issues with using XPATH 2.0 with Selenium 1.0 but seems to be working fine with 2.0.


2

Though you would be missing testing calendar functionality but if nothing works (as it is not just clicking calendar link but you would also have to navigate to right place in calendar) and it becomes a blocker then try typing directly in text box.


2

If you're already using XPath for this, can you start your XPath earlier on, for example, from the table that it appears as though this is in.


2

As User246 mentioned, the xpath support depends on the browser. I have found a few problems with using XPath cross-browser. My solution for this problem was to find elements by tag name first, then iterate through the list of elements matching the tag and look for the attributes that I want to match on as well.


2

You've discovered one of the many flaws in automation, especially record n play automation. Either it make sure the app you are testing never changes its identifiers or you don't use record and replay and use code that can be more 'intelligent'



Only top voted, non community-wiki answers of a minimum length are eligible