appium实用xpath定位元素-打开关闭按钮

appium实用xpath定位元素-打开关闭按钮

一、实用xpath定位,定位菜单框中的5个元素

表达式:    //android.widget.HorizontalScrollView/*/android.support.v7.app.a$c

但是在appium中无法识别“$”,要改为点“.”或者星“*”,所以要改为: //android.widget.HorizontalScrollView/*/*

//获取菜单通过xpath:
public void clickMenuByXpath(int index){
    driver.findElements(By.xpath("//android.widget.HorizontalScrollView/*/*")).get(index).click();
    //driver.findElement(By.xpath("//android.widget.HorizontalScrollView/*/*["+index+"]")).click();两种写法
}	

//元素上滑动
public void elementFromSwipe() throws Exception{
    clickMenuByXpath(2);
    //直接使用xpath定位第3个元素
    //driver.findElement(By.xpath("//android.widget.HorizontalScrollView/*/*[2]")).click();
    AndroidElement element = driver.findElementById("com.zhihu.android:id/recycler_view");
    AppiumUtils aUtils=new AppiumUtils(driver);
    for(int i=0;i<3;i++){
    	Thread.sleep(1000);
    	aUtils.swipeOnElement(element, "right", 10, 10, 500);
    }
    System.out.println("滑动成功");
}

二、进入设置——》滑屏找到账户与安全——》找到新浪微博,并根据新浪微博找到开关,并对开关进行操作

appium实用xpath定位元素-打开关闭按钮

    //社交账户通过xpath定位,如通过新浪微博定位开关
	public void useSocialAccount() throws Exception{
		clickMenuByXpath(4);
		Thread.sleep(1000);
		driver.findElement(By.xpath("//*[@text='设置']")).click();
		System.out.println("打开设置成功");
		//因为账户与安全在设置的最下面需要翻动一屏,所以先判断是否存在,不存在滑动屏幕,为了防止死循环找10次找不到就算了
		AppiumUtils appiumUtils=new  AppiumUtils(driver);
		boolean flag=appiumUtils.isElementExist(By.xpath("//*[@text='帐号与安全']"));
		int conut=10;
		while (conut>0) {
			if(flag){
				driver.findElement(By.xpath("//*[@text='帐号与安全']")).click();
				conut=-1;//找到直接结束
			}else {
				appiumUtils.swtipe("up", 1500);
				flag=appiumUtils.isElementExist(By.xpath("//*[@text='帐号与安全']"));
				conut--;
			}
		}
		//判断这个是否开关
		AndroidElement element= driver.findElement(By.xpath("//*[@text='新浪微博']/../following-sibling::*[1]/android.widget.Switch"));
		String checkedOld=element.getAttribute("checked");
		element.click();
		String checkedNew=element.getAttribute("checked");
		if(checkedOld.equals(checkedNew)){
			System.out.println("状态切换失败");
			//添加失败截图
			File file=driver.getScreenshotAs(OutputType.FILE);
			FileUtils.copyFile(file,new File("images/useSocialAccount.png"));//创建images
		}else {
			if(checkedNew.equals("true")){
				System.out.println("打开新浪微博成功");
				File file=driver.getScreenshotAs(OutputType.FILE);
				FileUtils.copyFile(file,new File("images/openuseSocialAccount.png"));//创建images
			}else {
				System.out.println("关闭新浪微博成功");
				File file=driver.getScreenshotAs(OutputType.FILE);
				FileUtils.copyFile(file,new File("images/closeuseSocialAccount.png"));//创建images
			}
		}
	}

下面的例子也适用这种情况:

appium实用xpath定位元素-打开关闭按钮

	//消息推送设置,通过xpath定位,通过知乎特别推荐定位开关,并操作
	public void messagePush() throws Exception{
		clickMenuByXpath(4);
		Thread.sleep(1000);
		driver.findElement(By.xpath("//*[@text='设置']")).click();
		System.out.println("进入推送消息设置成功");
		//因为账户与安全在设置的最下面需要翻动一屏,所以先判断是否存在,不存在滑动屏幕,为了防止死循环找10次找不到就算了
		AppiumUtils appiumUtils=new  AppiumUtils(driver);
		boolean flag=appiumUtils.isElementExist(By.xpath("//*[@text='推送消息设置']"));
		int conut=10;
		while (conut>0) {
			if(flag){
				driver.findElement(By.xpath("//*[@text='推送消息设置']")).click();
				conut=-1;//找到直接结束
			}else {
				appiumUtils.swtipe("up", 1500);
				flag=appiumUtils.isElementExist(By.xpath("//*[@text='推送消息设置']"));
				conut--;
			}
		}
		//判断这个是否开关
		AndroidElement element= driver.findElement(By.xpath("//*[@text='知乎特别推荐']/../following-sibling::*[1]/android.widget.CheckBox"));
		String checkedOld=element.getAttribute("checked");
		element.click();
		String checkedNew=element.getAttribute("checked");
		if(checkedOld.equals(checkedNew)){
			System.out.println("勾选失败");
			//添加失败截图
			File file=driver.getScreenshotAs(OutputType.FILE);
			FileUtils.copyFile(file,new File("images/messagePush.png"));//创建images
		}else {
			if(checkedNew.equals("true")){
				System.out.println("打开勾选成功");
				File file=driver.getScreenshotAs(OutputType.FILE);
				FileUtils.copyFile(file,new File("images/messagePush.png"));//创建images
			}else {
				System.out.println("关闭成功");
				File file=driver.getScreenshotAs(OutputType.FILE);
				FileUtils.copyFile(file,new File("images/messagePush.png"));//创建images
			}
		}
	}