selenium常用命令之页面元素定位
WebDriver driver= new ChromeDriver();
<input type="text" id="phone" name="phone" class="LoginText" placeholder="手机号">
1、使用id定位(idToFind)
WebElement byId=driver.findElement(By.id("phone"));
System.out.println(byId.getText());
2、使用Name属性
WebElement byName=driver.findElement(By.name("phone"));
System.out.println(byName.getText());
<a href="http://www.hao123.com" name="tj_trhao123" class="mnav">hao123</a>
3、使用超链接
WebElement byLinkText=driver.findElement(By.linkText("hao123"));
System.out.println(byLinkText.getText());
WebElement byPartialLinkText=driver.findElement(By.partialLinkText("hao"));
System.out.println(byPartialLinkText.getText());
5、使用Class属性
WebElement byClassName=driver.findElement(By.className("mnav"));
System.out.println(byClassName.getText());
6、按标记名称:TagName
WebElement byTagName=driver.findElement(By.tagName("a"));
System.out.println(byTagName.getText());
7、按路径:Xpath
WebElement byXpatn=driver.findElement(By.xpath("//*[@id='u1']/a[3]"));
System.out.println(byXpatn.getText());
8、定位数组
List<WebElement> byList=driver.findElements(By.xpath("//*[@id='u1']/a[3]"));
System.out.println(.get(0).getText());