How to write xpath for this field? - java

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");

Related

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

Find an element by ID where the ID is dynamic

I have this code
WebElement element = driver.findElement(By.id("j_idt9:usuario"));
I want to know how I can use XPath to search for a specific ID. In this case, I want to search for the "usuario" portion because the "j_idt9" portion changes. I know that it can be done using a CSS Selector but I am required to use XPath.
You should tell your professor that CSS selectors are not just for classes... and if s/he really did say that, they should be embarrassed and go do some reading. They can start with these two references:
https://www.w3.org/TR/selectors/#selectors
https://saucelabs.com/resources/articles/selenium-tips-css-selectors
If you have to use XPath, it would look like
"//*[ends-with(#id, 'usuario')]"
You'd be better off using a CSS selector because it's faster and has better, more consistent browser support. It would look like
"[id$='usuario']"
In general, your locator strategy should look like this...
By.id
By.linkText, By.partialLinkText
By.cssSelector
then as a last resort, By.xpath
By.xpath should only be used when the other methods won't work. Cases like you need to find an element by the text it contains or you have to do some parent/child shifts that CSS can't do.

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

My web driver does not understand xpath

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

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