Hot answers tagged

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

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?


9

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

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

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

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

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

It might be issue of same level HTML code. Solution : 1. For First Dropdown : (//div[@name='expiryDate']/div/a/div[2]/i)[1] 2. For Second Dropdown : (//div[@name='expiryDate']/div/a/div[2]/i)[2] 3. Use element list to get Dropdown : List<WebElement> lstDrp = driver.findElements(By.xpath("//div[@name='expiryDate']/div/a/div[2]/i")); lstDrp.get(...


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

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

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


4

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-


4

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


4

For the password you could try: //input[@type='password'][not(@style)] For the username : //input[@type='text'][not(@style)] This will select all input elements where the type attribute has the value 'password' (or 'text'), that do not have the style attribute defined. Ofcourse these are not super precise yet, you could additionally check whether there ...


4

An absolute XPath should start with a slash /: '/html/body/div[2]/div[1]/div/div[1]'. It is advised to use relative XPath like: '//div[@id="portfolioName"]'. For this to work, the id has to be unique for div tags. However if the id is unique, you can better identify the object by id and not XPath: driver.findElement(By.id("portfolioName"))


4

Because your Xpath is an absolute Xpath (that is, you start at the top of the HTML DOM and trace through all the elements), it's very fragile. If any part of the DOM above the element you're looking for is dynamic, your Xpath breaks and the element can't be found. That means, for instance if one of the tables inserts a new row with a message, instead of row[...


4

Avoid the use of 'Absolute XPath' if possible in your coding to avoid future rework if there is any change in page structure. If your element is having attributes like class, name & Type then try to locate the element with the use of Relative XPath. In your case, you can try with the following element locators- Most suitable & fast element locator ...


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