简体   繁体   English

如何使用WebDriver处理Appium中的弹出窗口

[英]How to handle pop up window in appium with webdriver

I unable to click Open button and getting an exception as "No such element exception". 我无法单击“打开”按钮,并且得到一个异常,例如“无此类元素异常”。 I tried all XPath and id but same exception I am getting. 我尝试了所有XPath和ID,但遇到了相同的异常。 I have tried getContextHandles() and it returns size as one. 我已经尝试过getContextHandles(),它返回大小为1。 it means control cannot switch. 这意味着控制无法切换。 Kindly help me to click the "Open" button. 请帮助我单击“打开”按钮。

Below are the xPath i tried. 以下是我尝试过的xPath。

By.name("Open");
By.xpath("//android.widget.Button[@index='1']");
By.xpath("//android.widget.Button[@resource-id='org.hola:id/btn_open']");
By.xpath("//android.widget.Button[@resource-id='org.hola:id/btn_open' and @index='1' and @text='Open']");

在此处输入图片说明

在此处输入图片说明

如果启动任何应用程序后都会弹出此窗口,则必须在代码中设置“ appActivity”和“ appPackage”功能。

Try MobileBy instead of By 尝试使用MobileBy代替By

MobileBy.name("Open");
MobileBy.xpath("//android.widget.Button[@index='1']");
MobileBy.xpath("//android.widget.Button[@resource-id='org.hola:id/btn_open']");
MobileBy.xpath("//android.widget.Button[@resource-id='org.hola:id/btn_open' and @index='1' and @text='Open']");

Locator can be identified by using UIAutomator viewer tool. 可以使用UIAutomator查看器工具来识别定位 Following code setup use to handles the pop-up window in Appium 以下代码设置用于处理Appium中的弹出窗口

WebDriver driver;
@BeforeTest
static void setup() throws MalformedURLException{
    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability(CapabilityType.PLATFORM, "Android");
    cap.setCapability(CapabilityType.VERSION, "Android-version"); 
    cap.setCapability("deviceName", "connected device name");
    cap.setCapability("appPackage", "App package name");
    cap.setCapability("appActivity", "App activity");
    driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap); 
}
@Test
static void Selendroid() throws InterruptedException{
    Thread.sleep(2000);
    driver.findElement(By.name("Locator")).click();
    System.out.println(driver.findElementById("android:id/message").getText());
    driver.findElement(By.name("Locator")).click();
}

For Browser alerts use driver.switchTo().alert() method to Handle 对于浏览器警报,请使用driver.switchTo().alert()方法处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM