Locator types Flashcards
Use XPath to find div with id “content” which contains table, navigate to input in the 3rd td of the second tr, and enter number 100 into input.
driver.findElement(
By.xpath(“.//*[@id = ‘content’]/table/tr[2]/td[3]/input”))
.sendkeys(“100”);
How to find an HTML tag using XPath? Show example.
with //
Example: driver.findElement(By.xpath(“//form”)
Using XPath to find a form with action “blah”
driver.findElement( By.xpath("//form[@action='blah']");
What “.” means in XPath?
. represents the current element.
For example:
.//input[@name=’radio’] and //input[@name=’radio’]
The first statement looks for all “inputs” with name=’radio’ beneath the actual element.
The second statement looks globally for all input elements with the name ‘radio’.
How to specify any element using XPath?
With
*for example:
driver.findElement(By.xpath(".//*[@id = 'content']");
What are / and . in XPath?
They represent absolute and relative XPaths (/ vs .)
/ introduces an absolute location path, starting at the root of the document.
. introduces a relative location path, starting at the context node.
How to search by 2 attributes using XPath?
driver.findElement(By.xpath(“//input[@name=’name’][@value=’some-value’]”);
What “*” means in XPath?
Any tag, for example: driver.findElements(By.xpath(“.//*[@name = ‘radio-buton’]”));
How to use OR and AND operators in XPath?
//input[@id=’name’ or @name=’name’]
Using XPath, how to find “a” link with text “Click Me”
//a[text()=’Click Me’]
Using XPath, how to find any element with text “Click Me”
//*[text()=’Click Me’]
Using XPath, how to find element containing partial text
//tagname[contains (@Attribute, ‘Value’)]
or using Text:
//tagname[contains(text(),'XYZ')]
How to find elements by multiple class names?
By.cssSelector(“.class1.class2”)
or
By.cssSelector(“div.class1.class2”)
or
By.xpath(“//div[contains(@class, ‘class1’) and contains(@class, ‘class2’)]”
How to maximize window using WebDriver?
driver.manage().window().maximize();