- Selenium Tutorial
- Selenium Useful Resources
- Selected Reading
Selenium Locators. Locating by ID Refer Part2-Page37. Driver.findElement(By.id('q')).sendKeys('Selenium 3'). Download the Selenium WebDriver Cheat Sheet Cheat Sheet. PDF (recommended) PDF (5 pages) Alternative Downloads. PDF (black and white) LaTeX. Narayanan Palani. Languages: English. Cheat Sheet 6: All Selenium This cheat sheet uses the most frequently used Selenium and Python. It will give you clear instructions for Auto downloading, locating elements, and reading browser details. It has a list of Python Selenium commands for operations on elements including code snippets so you can if you are doing it right.
Selenium WebDriver Cheat Sheet. Qtp qtp guru99 guru99 guru99 guru99 driven driven driven standalone javascriptexecutor locators toolsqa toolsqa. For this reason, having a good cheat sheet is always handy. Click on the image below, and you will find a neat cheat sheet from Redgate, with CSS, XPath, and DOM included. Bonus tip: use a fail-safe lookup algorithm. Let’s admit it, even if you have years of experience and follow best practices, Selenium locators will always be brittle.
Locating elements in Selenium WebDriver is performed with the help of findElement() and findElements() methods provided by WebDriver and WebElement class.
findElement() returns a WebElement object based on a specified search criteria or ends up throwing an exception if it does not find any element matching the search criteria.
findElements() returns a list of WebElements matching the search criteria. If no elements are found, it returns an empty list.
The following table lists all the Java syntax for locating elements in Selenium WebDriver.
Method | Syntax | Description |
---|---|---|
By ID | driver.findElement(By.id (<element ID>)) | Locates an element using the ID attribute |
By name | driver.findElement(By.name (<element name>)) | Locates an element using the Name attribute |
By class name | driver.findElement(By.className (<element class>)) | Locates an element using the Class attribute |
By tag name | driver.findElement(By.tagName (<htmltagname>)) | Locates an element using the HTML tag |
By link text | driver.findElement(By.linkText (<linktext>)) | Locates a link using link text |
By partial link text | driver.findElement(By.partialLinkText (<linktext>)) | Locates a link using the link's partial text |
By CSS | driver.findElement(By.cssSelector (<css selector>)) | Locates an element using the CSS selector |
By XPath | driver.findElement(By.xpath (<xpath>)) | Locates an element using XPath query |
Locators Usage
Now let us understand the practical usage of each of the locator methods with the help of https://www.calculator.net
By ID
Here an object is accessed with the help of IDs. In this case, it is the ID of the text box. Values are entered into the text box using the sendkeys method with the help of ID(cdensity).
By Name
Here an object is accessed with the help of names. In this case, it is the name of the text box. Values are entered into the text box using the sendkeys method with the help of ID(cdensity).
By Class Name
Here an object is accessed with the help of Class Names. In this case, it is the Class name of the WebElement. The Value can be accessed with the help of the gettext method.
By Tag Name
The DOM Tag Name of an element can be used to locate that particular element in the WebDriver. It is very easy to handle tables with the help of this method. Take a look at the following code.
By Link Text
This method helps to locate a link element with matching visible text.
By partial link text
This methods helps locate a link element with partial matching visible text.
By CSS
The CSS is used as a method to identify the webobject, however NOT all browsers support CSS identification.
By XPath
XPath stands for XML path language. It is a query language for selecting nodes from an XML document. XPath is based on the tree representation of XML documents and provides the ability to navigate around the tree by selecting nodes using a variety of criteria.
- Selenium Tutorial
- Selenium Useful Resources
- Selected Reading
Locating elements in Selenium WebDriver is performed with the help of findElement() and findElements() methods provided by WebDriver and WebElement class.
findElement() returns a WebElement object based on a specified search criteria or ends up throwing an exception if it does not find any element matching the search criteria.
findElements() returns a list of WebElements matching the search criteria. If no elements are found, it returns an empty list.
The following table lists all the Java syntax for locating elements in Selenium WebDriver.
Method | Syntax | Description |
---|---|---|
By ID | driver.findElement(By.id (<element ID>)) | Locates an element using the ID attribute |
By name | driver.findElement(By.name (<element name>)) | Locates an element using the Name attribute |
By class name | driver.findElement(By.className (<element class>)) | Locates an element using the Class attribute |
By tag name | driver.findElement(By.tagName (<htmltagname>)) | Locates an element using the HTML tag |
By link text | driver.findElement(By.linkText (<linktext>)) | Locates a link using link text |
By partial link text | driver.findElement(By.partialLinkText (<linktext>)) | Locates a link using the link's partial text |
By CSS | driver.findElement(By.cssSelector (<css selector>)) | Locates an element using the CSS selector |
By XPath | driver.findElement(By.xpath (<xpath>)) | Locates an element using XPath query |
Locators Usage
Now let us understand the practical usage of each of the locator methods with the help of https://www.calculator.net
By ID
Here an object is accessed with the help of IDs. In this case, it is the ID of the text box. Values are entered into the text box using the sendkeys method with the help of ID(cdensity).
By Name
Here an object is accessed with the help of names. In this case, it is the name of the text box. Values are entered into the text box using the sendkeys method with the help of ID(cdensity).
By Class Name
Here an object is accessed with the help of Class Names. In this case, it is the Class name of the WebElement. The Value can be accessed with the help of the gettext method.
By Tag Name
The DOM Tag Name of an element can be used to locate that particular element in the WebDriver. It is very easy to handle tables with the help of this method. Take a look at the following code.
By Link Text
This method helps to locate a link element with matching visible text.
Css Selectors Selenium Cheat Sheet
By partial link text
This methods helps locate a link element with partial matching visible text.
By CSS
Selenium Xpath Cheat Sheet
The CSS is used as a method to identify the webobject, however NOT all browsers support CSS identification.
By XPath
XPath stands for XML path language. It is a query language for selecting nodes from an XML document. XPath is based on the tree representation of XML documents and provides the ability to navigate around the tree by selecting nodes using a variety of criteria.