我们以百度主页搜索框为例:=
<input autocomplete="off" maxlength="255" value="" class="s_ipt" name="wd" id="kw">
1.通过id定位元素
findElement(By.id("id_vaule"))
driver.findElement(By.id("kw")).sendKeys("123456");
WebElement bu = driver.findElement(By.id("su"));
bu.click();
2.通过name定位元素:
findElement(By.name("name_vaule"))
driver.findElement(By.name("wd")).sendKeys("123456");
3.通过className定位元素(样式名):
findElement(By.className("class_name"))
driver.findElement(By.className("s_ipt")).sendKeys("123456");
4.通过tag_name定位元素(标签名):
findElement(By.tagName("tag_name"))
driver.findElement(By.tagName("input")).sendKeys("123456");
5.通过link定位:
findElement(By.linkText("text_vaule"))
或:findElement(By.partialLinkText("text_vaule"))
driver.findElement(By.linkText("糯米")).click();
driver.findElement(By.partialLinkText("糯")).click();
driver.findElement(By.partialLinkText("新")).click();
6.通过css定位元素:
findElement(By.cssSelector())
7.通过xpath定位元素:
findElement(By.xpath())