Se (5)-location element
package testWd;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class findEle {
@Test
public void findElement() throws InterruptedException {
System.setProperty("webdriver.firefox.bin", "D:/Firefox/firefox.exe");
WebDriver dr = new FirefoxDriver();
dr.manage().window().maximize();
dr.get("http://www.baidu.com");
//1.id
/*WebElement el1 = dr.findElement(By.id("kw"));
el1.sendKeys("English");*/
//2.name
/*WebElement el1 = dr.findElement(By.name("wd"));
el1.sendKeys("Chinese");*/
//3.className
/*WebElement el1 = dr.findElement(By.className("s_ipt"));
el1.sendKeys("Spain");*/
//4.cssSelector
/*WebElement el1 = dr.findElement(By.cssSelector("#kw"));
el1.sendKeys("Taiwan");*/
/*WebElement el1 = dr.findElement(By.cssSelector(".s_ipt"));
el1.sendKeys("shanghai");*/
/*WebElement el1 = dr.findElement(By.cssSelector(".quickdelete-wrap input"));
el1.sendKeys("beijing");*/
//5.linktext
/*WebElement el1 = dr.findElement(By.linkText("hao123"));
el1.click();*/
//6.partial linktext
/*WebElement el1 = dr.findElement(By.partialLinkText("hao1"));
el1.click();*/
//7.tagname, not recommended, because if there are multiple tags on the page, only the first tag will be selected
/*dr.get("https://passport.cnblogs.com/user/signin");
WebElement el1 = dr.findElement(By.tagName("a"));
el1.click();*/
//Using xpath, there are three options:
//first choice, first search id,name
//Second option, use hierarchical positioning, first locate the parent node, and then locate the child node through the parent node
//third option, use document positioning, because if an element changes or the hierarchy changes, it will not work
//8.xpath, format://tag [@ attribute ='']
/*WebElement el1 = dr.findElement(By.xpath("//input[@id='kw']"));
el1.sendKeys("qinhuangdao");*/
//xpath,id, tag can be omitted, simple to write, but efficiency may be reduced
/*WebElement el1 = dr.findElement(By.xpath("//*[@id='kw']"));
el1.sendKeys("jinan");*/
//xpath,name
/*WebElement el1 = dr.findElement(By.xpath("//*[@name='wd']"));
el1.sendKeys("tianjin");*/
//hierarchical positioning, non-xpath, first position parent node, then position child node through parent node
/*WebElement el1 = dr.findElement(By.id("form")).findElement(By.id("kw"));
el1.sendKeys("gansu");*/
/*WebElement el1 = dr.findElement(By.id("form")).findElement(By.xpath("span/input"));
el1.sendKeys("xinjiang");*/
//hierarchical positioning,xpath, first position parent node, then position child node through parent node-------pseudo-documented hierarchical positioning
/*WebElement el1 = dr.findElement(By.xpath("//*[@id='form']/span/input"));
el1.sendKeys("heilongjiang");*/
//xpath, document location
WebElement el1 = dr.findElement(By.xpath("/html/body/div[2]/div/div/div/div/form/span/input"));
el1.sendKeys("laiwu");
//Thread.sleep(3000);
//dr.quit();
}
}