gpt4 book ai didi

java - Android 应用程序自动化脚本 : Through Appium, Selenium WebDriver,Java:NoSuchElementFoundException

转载 作者:行者123 更新时间:2023-12-01 09:39:38 26 4
gpt4 key购买 nike

我正在尝试在 Android 设备中打开移动 .apk 文件,然后选择国家/地区“美国”并单击“保存”按钮。

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;

import io.appium.java_client.android.AndroidDriver;

public class DemoC {

AndroidDriver driver =null;
DesiredCapabilities capabilities;
File app = new File("C:\\Users\\310250972\\workspace\\DreamMapper_2.5_Alpha_July_5_65.apk");

@Test(priority=1)
public void invokeApp() throws MalformedURLException
{
capabilities = new DesiredCapabilities();
capabilities.setCapability("automationName","Appium" );
capabilities.setCapability("platformName","Android" );
capabilities.setCapability("platformVersion","6.0.1");
capabilities.setCapability("deviceName","Galaxy S6" );

capabilities.setCapability("app", app.getAbsolutePath());

//capabilities.setCapability("appPackage","com.philips.sleepmapper.root");
//capabilities.setCapability("appactivity","com.philips.sleepmapper.activity.SplashScreenActivity");

driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

}

@Test(priority=2)
public void selectCountry() throws InterruptedException{
Thread.sleep(20000);
//driver.findElement(By.xpath("//android.widget.TextView[Contain]"))
driver.findElement(By.xpath("//android.widget.TextView[contains(@text='United States')]")).click();
Thread.sleep(10000);
driver.findElement(By.xpath("//android.widget.TextView[contains(@text='Save')]")).click();
// driver.findElement(By.name("Save")).click();
}

}[I want to select "United States" and click on "Save" button][1]

//执行时,测试方法“selectCountry()”失败。没有找到这样的元素异常

最佳答案

当您将 Xpath 查询与 contains 一起使用时,您将需要使用逗号分隔符而不是等于。

因此,您的步骤应如下所示:

driver.findElement(By.xpath("//android.widget.TextView[contains(@text, 'United States')]")).click();
Thread.sleep(10000);
driver.findElement(By.xpath("//android.widget.TextView[contains(@text, 'Save')]")).click();

如果这没有帮助,您可以尝试使用 driver.getPageSource(); 来确保元素实际上具有预期的 @text 值。该命令返回所有现有元素,可用于验证元素是否存在且属性正确。

关于java - Android 应用程序自动化脚本 : Through Appium, Selenium WebDriver,Java:NoSuchElementFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38564238/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com