Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Hot answers tagged

21

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 ...


19

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')]


13

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 ...


12

At this situation We have lots of options- Option 1 : Look for any other attribute which Is not changing every time In that div node like name, class etc. So If this div node has class attribute then we can write xpath as bellow. //div[@class='post-body entry-content']/div[1]/form[1]/input[1] Option 2 : We can use absolute xpath (full xpath) where you ...


12

Do you think that time is the problem here? If so try to increase the time you are providing in Thread.sleep(); and see it that works out for you. If its not the time, then did you try to find out what the problem is? Are you getting any error?


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

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.


8

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   ...


8

The character ' is an apostrophe. The colon character is :. Selenium unfortunately only supports XPath 1.0, so this is tricky. You can do it by escaping the ' with a backslash like this: By.xpath("//span[@trans=\"'WbOrderHeader.OneCancelsAnother\"]")


7

Avoid the use of 'Absolute XPath' if possible in your coding. If your element is having with class, ID, name & Type attributes then try to locate the element with the use of Relative XPath. You can use- XPath: //input[@id='portfolioName'] OR XPath: //input[@id='portfolioName'][@type='text'] OR Xpath=//input[@type='text' AND @name='portfolioName[]'...


6

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:...


6

There are much better ways to locate the element aside from using XPath, don't focus on XPath too much - this is, generally speaking, the slowest and the least readable way to locate elements. I would locate the element by the link text instead, example in Protractor/WebDriverJS: element(by.linkText("Sign Out")).click(); Example in Java: driver....


6

Whenever you are trying to locate Hyperlink Element & you have '/a' attribute, just forget about the XPath method and try using linkText element Locator. <html> <head> <title>My Page</title> <body> <a href="http://www.google.com">Google</a> <body> </html> In this situation ...


6

Because in your first example all your span elements reside on the same level so that they all have different positions. However in your second example each span resides within its own container (<b>) where each of them has the position=1, hence all the elements are returned. To return first span in your second example you can use the xpath like this: ...


5

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


5

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, ...


5

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 ...


5

Use contains() or starts-with() function in XPath: Solution: With contains(): //div[contains(@id,'post-body')]/div[1]/form[1]/input[1] With starts-with(): //div[starts-with(@id,'post-body')]/div[1]/form[1]/input[1]


5

Use other elements and attributes for partial and combination matches Generally in these cases of database backed and changing ID's in web element selectors your options are to look for one or more of: a higher level selector such as the form name, e.g. //form/input[@type='submit'] a combo of non-unique selectors that together are unique e.g. //form[@class=...


5

You may use this- Xpath1= //a[contains(.,'Create a Rediffmail account')] OR Xpath2=//a[contains(@href,'details')] OR Xpath3=.//*[@id='toprightinfo']/a[2] Or you try with other Locator types as- LinkText Check I have tried & found 1 match xpath-


5

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 ...


5

Try this xpath //*[contains(@class,'btn btn-primary btn-lg btn-block pink button') and contains(@tabindex,'4')] The xpath basically means : any element that has class attribute containing 'btn btn-primary btn-lg btn-block pink button' and a tabindex attribute containing 4. You should try to read a little about how to write custom xpaths as addons like ...


5

When there are multiple instances and there is no other way to distinguish them* you can refer to the instance index, i.e. 'which one'. CSS a.instanceLink:nth-child(1) or a.instanceLink:nth-child(2) XPath Note: 0 based index a[@class='instanceLink'][0] or a[@class='instanceLink'][1] You can see these in chrome tools, e.g. css - $('a.instanceLink:...


5

In my case, How I worked on this same scenario: Suppose if you have 3 buttons with the same XPath-like in my DOM: <input type="submit" id="button" value="Edit"/> <input type="submit" id="button" value="Edit"/> <input type="submit" id="button" value="Edit"/> The best option to overcome this situation is: 1. By XPath indexing option: By....


5

As far as i know xpath doesn't depends on any browser. Make sure you have created correct xpath it will work. Second : It's depend on your tag which attribute it has and how efficient you are in xpath. For example this is the simple hyperlink: <a href="https://stackexchange.com/questions?tab=hot" name="hotnetwork" class="js-gps-track">Hot Network ...


5

Same like posted here. //*[starts-with(@id,'post-')]/h3/a/time OR //*[contains(@id,'post-')]/h3/a/time try with other attributes which are not changing continuously.


5

The two paths might give the same result on one sample document, but they will give different results on other documents. The one that is best is the one that gives the right results over a whole class of documents. Without knowing anything about the structure and semantics of the class of documents you want to apply these expressions to, it's impossible to ...


4

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.


4

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 ...


4

can you please try the options below: By Xpath: button[@type='submit'] button[@class='btn btn-success'] button[@type='submit'][@class='btn btn-success'] By css selector: button[type='submit'] button[class='btn btn-success'] button[type='submit'][class='btn btn-success'] Any one expression above should locate this element, please tell me if it still ...


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