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.
Related
Is there a away to get the element input#fakebox-input and locate it. I was thinking using By.cssSelector or By.xpath, but I don't know how I can include input#fakebox-input
Please give more details when asking questions, not sure what is the ask here ?
If you want help creating xpath or CSS may be show the developer console(f12), anyway , if I go to google and inspect the search, there is an input of a class, based on that, this xpath works, so if you have something similar, replace glFYF gsfi with your class name --
//input[contains(#class,'gLFyf gsfi')]
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
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
I have an element extracted from the DOM using JSOUP. I want to get the CSS selector of that element, so I can quickly find the equivalent elements on other pages with the same structure. Is this possible?
Thanks
I doubt that it's possible, because multiple selectors could be valid for your element -- eg, the trivial Selector.select("*",rootElement) would match it.
It sounds like you don't want to use the same element-extraction code (that you initially used) for all subsequent documents? If you're intent on using selectors, then perhaps try different ones until you find one that you're happy with (from either a readability or a performance perspective).
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.