1 Packagecom.mypro.jase;2 3 Importorg.openqa.selenium.By;4 ImportOrg.openqa.selenium.WebDriver;5 Importorg.openqa.selenium.WebElement;6 7 Public classfindelement {8 Webdriver driver;9String url = "Http://www.baidu.com";Ten One /** A * Positioning Element 1:findelement (By.id ()) - */ - Public voidFindByID () { the driver.get (URL); - //Locate the Baidu search input box by ID and enter "seleNium" -webelement element = Driver.findelement (by.id ("kw"))); -Element.sendkeys ("Selenium"); + } - + /** A * element Positioning 2:findelement (By.name ()) at */ - Public voidFindbyname () { - driver.get (URL); - //Locate the Baidu search input box by name and enter "Java" -webelement element = Driver.findelement (By.name ("WD")); -Element.sendkeys ("Java"); in } - to /** + * element Positioning 3:findelement (By.classname ()) - */ the Public voidFindbyclassname () { * driver.get (URL); $ //Locate Baidu search input box via classname and enter JavaPanax Notoginsengwebelement element = Driver.findelement (By.classname ("S_ipt")); -Element.sendkeys ("Java"); the } + A /** the * element Positioning 4:findelement (By.tagname ()) + */ - Public voidFindbytagname () { $ driver.get (URL); $ //Locate the Baidu search input box by tagname (label) and enter "Java" -webelement element = Driver.findelement (by.tagname ("Input")); -Element.sendkeys ("Java"); the } - Wuyi /** the * element Positioning 5:findelement (By.linktext ()) - */ Wu Public voidFindbylinktext () { - driver.get (URL); About //Navigate to the Hao123 button by Linktext (hyperlink) and click; This is a precise search $webelement element = Driver.findelement (By.linktext ("http://www.hao123.com")); - Element.click (); - } - A /** + * element Positioning 6:findelement (By.partiallinktext ()) the */ - Public voidFindpartaillinktext () { $ driver.get (URL); the //Navigate to the Hao123 button by Partaillinktext (hyperlink) and click; This is a fuzzy lookup thewebelement element = Driver.findelement (By.partiallinktext ("ao123")); the Element.click (); the } - in /** the * element Positioning 7:findelement (By.xpath ()) the */ About Public voidFindbyxpath () { the driver.get (URL); the //Navigate to the Baidu input box via XPath and enter "XPath" thewebelement element = Driver.findelement (By.xpath (".//*[@id = ' kw ']")); +Element.sendkeys ("XPath"); - } the Bayi /** the * element Positioning 8:findelement (By.cssselector ()) the */ - Public voidFindbycssselector () { - driver.get (URL); the //Navigate to the Baidu input box via CSS and enter "Css_selector" thewebelement element = Driver.findelement (By.cssselector ("#kw")); theElement.sendkeys ("Css_selector"); the } -}
Summarize:
Selenium's Webdriver provides eight basic element positioning methods.
1, through the ID location: findelement (By.id ());
2, through the name positioning: Findelement (By.name ());
3, through the Class positioning: Findelement (By.classname ());
4, through the tag positioning: findelement (By.tagname ());
5, through the link positioning: Findelement (By.linktext ());
6, through the Partiallink positioning: findelement (By.partiallinktext ());
7, through the XPath positioning: findelement (By.xpath ());
8, through the CSS positioning: findelement (By.cssselector ());
"Selenium 3+java Automation (4)"-Eight element positioning