您好,登錄后才能下訂單哦!
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,不推薦使用,因?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();
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。