package testWd;
創(chuàng)新互聯(lián)秉承實(shí)現(xiàn)全網(wǎng)價(jià)值營(yíng)銷(xiāo)的理念,以專(zhuān)業(yè)定制企業(yè)官網(wǎng),成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作,小程序定制開(kāi)發(fā),網(wǎng)頁(yè)設(shè)計(jì)制作,成都做手機(jī)網(wǎng)站,成都全網(wǎng)營(yíng)銷(xiāo)幫助傳統(tǒng)企業(yè)實(shí)現(xiàn)“互聯(lián)網(wǎng)+”轉(zhuǎn)型升級(jí)專(zhuān)業(yè)定制企業(yè)官網(wǎng),公司注重人才、技術(shù)和管理,匯聚了一批優(yōu)秀的互聯(lián)網(wǎng)技術(shù)人才,對(duì)客戶(hù)都以感恩的心態(tài)奉獻(xiàn)自己的專(zhuān)業(yè)和所長(zhǎng)。
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,不推薦使用,因?yàn)轫?yè)面上如果有多個(gè),只會(huì)選擇第一個(gè)標(biāo)簽
/*dr.get("https://passport.cnblogs.com/user/signin");
WebElement el1 = dr.findElement(By.tagName("a"));
el1.click();*/
//使用xpath,有三個(gè)選擇:
//第一選擇,優(yōu)先查找id,name
//第二選擇,使用層級(jí)定位,先定位父節(jié)點(diǎn),再通過(guò)父節(jié)點(diǎn)定位子節(jié)點(diǎn)
//第三選擇,使用文檔定位,因?yàn)橹灰硞€(gè)元素變了或者層級(jí)變了,就不好使了
//8.xpath,格式://標(biāo)簽[@屬性='']
/*WebElement el1 = dr.findElement(By.xpath("http://input[@id='kw']"));
el1.sendKeys("qinhuangdao");*/
//xpath,id,標(biāo)簽可以省略,寫(xiě)起來(lái)簡(jiǎn)單,但效率可能會(huì)降低
/*WebElement el1 = dr.findElement(By.xpath("http://*[@id='kw']"));
el1.sendKeys("jinan");*/
//xpath,name
/*WebElement el1 = dr.findElement(By.xpath("http://*[@name='wd']"));
el1.sendKeys("tianjin");*/
//層級(jí)定位,非xpath,先定位父節(jié)點(diǎn),再通過(guò)父節(jié)點(diǎn)定位子節(jié)點(diǎn)
/*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");*/
//層級(jí)定位,xpath,先定位父節(jié)點(diǎn),再通過(guò)父節(jié)點(diǎn)定位子節(jié)點(diǎn)---------偽文檔化的層級(jí)定位
/*WebElement el1 = dr.findElement(By.xpath("http://*[@id='form']/span/input"));
el1.sendKeys("heilongjiang");*/
//xpath,文檔定位
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();
}
}