selenium代码练习(定位2)

几种另外的定位方法

xpath css

 

package cntest.byp;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

//定位元素
/*
* css定位
* xpath定位
*/

public class DingWei2 {
public static void main(String[]args){
//打开浏览器
WebDriver driver=new FirefoxDriver();

//最大化窗口
driver.manage().window().maximize();
//输入网址
// driver.get("http://192.168.1.100");
driver.get("http://192.168.1.100/index.php?m=user&c=public&a=login");
//等待
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

//css定位
// driver.findElement(By.cssSelector("input[name='keyword']")).sendKeys("iphone");


//xpath
//绝对路径
// driver.findElement(By.xpath("/html/body/div[1]/div[2]/div[2]/form/(div/input[1]")).sendKeys("iphone");


//相对路径
// driver.findElement(By.xpath("//div/input")).sendKeys("iphone");
// driver.findElement(By.xpath("//div/input[2]")).click();

//使用xpath通过文本内容定位,适用于a标签-----》//标签名[text()='文本内容']
//driver.findElement(By.xpath("//a[text()='登录']")).click();

//使用xpath通过属性定位----格式://标签名[@属性名='属性值']
//driver.findElement(By.xpath("//input[@type='submit']")).click();

//使用xpath通过模糊匹配来定位元素-------格式://标签名[contains(text(),'文本内容')]-----适用于a标签
//driver.findElement(By.xpath("//a[contains(text(),'中心')]")).click();

//使用xpath通过模糊匹配定位元素-------格式://标签名[contains(@属性名,'部分属性值')]

driver.findElement(By.id("username")).sendKeys("dongfbb");
driver.findElement(By.xpath("//input[contains(@placeholder,'请输入')]")).sendKeys("123456");

//对于使用submit()相当于回车操作,提交操作,必须保证登录按钮中存在type属性,且type值为submit
driver.findElement(By.xpath("//input[contains(@placeholder,'请输入')]")).submit();
//driver.findElement(By.xpath("//input[contains(@placeholder,'请输入')]")).clear();

}

}

 

posted on 2017-06-10 19:55  Meteorbai  阅读(171)  评论(0编辑  收藏  举报

导航