Hot answers tagged

18

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


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


9

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\"]")


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


7

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


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

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]


6

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


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

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

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

In XPath there are a couple of ways you could try this. Match by text (can be partial text too) (//label[contains(.,'Missing Applicant')])[1] Match by tag (if that helps isolate the label from other labels) (//label[@_ngcontent-c18])[1] Match by partial tag value (//label[contains(@for,'radio-button')])[1] Note the brackets surrounding the selectors ...


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


5

This could be a timing issue(as most of the scenario you already discussed) when you're going to find it would not be present at that time on DOM, so you should try using WebDriverWait to wait until this element could be present as below:- WebDriverWait wait = new WebDriverWait(driver, 10); WebElement el = wait.until(ExpectedConditions....


4

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


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

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


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

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


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


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