My web driver does not understand xpath - java

I am working on Selenium . I am new user to selenium implementation . Can anyone help me out for my following question here :
how will webdriver understand same xpath for different web element in a page?(Lets say button"A" and "B" has the same Xpath.)

For XPath, CSS, className, or any other selector that can have duplicates:
If you do driver.findElement(), it will find the first element on the page that matches.
If you do driver.findElements(), it will find all of the elements on the page that matches.

Think about using a different locator strategy. CssSelectors, id and name are all supported.
Also, you don't have to use the Xpath generated (I assume you are using Selenium IDE or Selenium Builder?). Generated XPath is often complex and not fit for purpose. YOu can easily create better simplier XPath to find the same object.
I recommend installing Firebug and Firepath to give you an insight as to how to create and try out different Xpath and CssSelector

Related

How to write xpath for this field?

enter image description here
driver.findElement(By.xpath("//input[#id='QuickSearchProduct']")).sendKeys("ddd");
I am trying to get xpath for this field but unbale to pass any text on here. what i am missing?
If the ID is unique in HTML DOM, use id not xpath.
Xpath is least preferable choice in Selenium due to not having native support in different browser.
driver.findElement(By.id("QuickSearchProduct")).sendKeys("ddd");
also put some delay to let the web element rendered fully, and then you can interact with it. Explicit waits comes handy in these cases.
You can use id directly.
driver.findElement(By.id("QuickSearchProduct").sendKeys("ddd");
Xpath:
driver.findElement(By.xPath("//input[#id='QuickSearchProduct']").sendKeys("ddd");

How can i locate a web element by using DOM locator using selenium webdriver java

even i don't know when we come to know that there is need to use DOM locator.
Please help me to getting this.
There are different Locator Strategies you can use in order to find the element.
The recommended one using global id attribute - Java equivalent would be driver.findElements(By.id("your id here"))
The next one would be CSS Selector, it gives more flexibility if your DOM is complex enough and there are no unique IDs
And last but not the least would be XPath - the most resource intensive and the slowest, but on the other hand the most powerful approach which can uniquely identify any element on the page

Selenium (Java): ordered list of webelements but with nested divs

I am working with JavaSE and Selenium WebDriver within Chrome. My task is to find a set of input fields and do stuff with them. The issue is that I have to do stuff in the presented order they are available on the web page.
So I would find them via XPATH, because that's what works in the given web page. Let's say that I have a set of inputs on the following path: .../form/div/div/div
However for reasons I cannot say, certain type of input fields (such as text and numbers) are in the following path: .../form/div/div
The problem is that one set of the inputs are one div 'deeper' than the others, so when I save them to a List<WebElement> with driver.findElements, i can't really save their order.
I thought of finding the inputs with id, but the id names have a space in it which Selenium apparently dislikes. I am not sure if relative XPATH could be of help in this case.
Your comments are appreciated.
I made the mistake of not reading enough about XPATH. What I was looking for was the 'and' operand within an xpath expression. If you are a beginner like me, please read about it on w3schools.
Basically the following code solved my issue, as a workaround:
driver.findElements(By.xpath("//input[#required=''] | //select[#required='']"));

Which type of locator (like ID, xpath, class name etc) is good to use in selenium automation

We can locate an element in webpage using ID, xpath, class name, tag, name, link text,attribute etc
Using which kind of locator selenium detects the element faster. Also which type of locator is good to use(faster to access and reliable)
In My Experience following is order when it comes to search time of objects.
Link Text : fastest way to search its so fast some time you have to use wait for controlling it and works same for all browsers But you need to make sure that the text content has been loaded so it will fail if your page is too much bulky
CSS path: Its Also very fast but slightly less faster then Link Text But believe me it is a nightmare to edit css paths as you won't be easily able to find if a user changed class name "xyz" to "btw"
ID/Name : ID is supposed to be unique for an element . IF your developers are following the standard way then it is best .its equal to CSS path when it comes to speed
Xpath: (Slowest of all and second problem with XPATH is every browser has Different syntax like IE it has some other type of expression when compared with FF or chrome I always avoid using Xpath) But if you have written strict xpaths then your code will work like a charm. Xpaths enhance your capability to get any elemnt and are the most widely used.
http://www.seleniumhq.org/docs/03_webdriver.jsp#locating-ui-elements-webelements
In My Experience following is order when it comes to search time of objects.
Link Text : fastest way to search its so fast some time you have to use wait for controlling it and works same for all browsers
CSS path: Its Also very fast but slightly less faster then Link Text
ID/Name : its equal to CSS path when it comes to speed
Xpath: Slowest of all and second problem with XPATH is every browser has Different syntax like IE it has some other type of expression when compared with FF or chrome
I always avoid using Xpath

How to access the text field that do not have id property in selenium

i am working in selenium i want access a text field and fill with any value but that field do not have any id attribute, so tell me how to locate that field.
You can use Cssselector or xpath.
you can find lots more on google . one useful link is -
http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/dotnet/Selenium.html
If you cannot add id to element, then you can use other options:
use other attributes (e.g.name or any other you have) - it is not reliable, because attributes can change in time. Example:
//div[#name='some_name']
use your HTML layout to locate element - it's even less reliable, because your HTML layout may be changed in time. Example:
//footer//div[position()=2]
In order to make it easy to write your custom XPath expression I would suggest to use FireFinder for FireBug FireFox plugin. It supports both CSS and XPath expressions and makes writing expressions really straightforward.
For typing into a textbox there is no need to have an ID. You can do it with XPath or CSS.

Categories