驱动程序不等待元素可见

时间:2016-07-14 05:11:08

标签: java appium

我在Appium中遇到此问题,其中AndroidDriver不等待catch块中的OK按钮(android.widget.Button[@text='OK'])出现。这是代码:

WebDriverWait wait = new WebDriverWait(driver, 120);
if(networkConnection.wifiEnabled()){
        try{
            WebElement msg = driver.findElementByXPath("//android.widget.TextView[@text='No network connection detected. Please check your Wi-Fi or mobile settings.']");
            if(msg.isDisplayed()){
                wait.until(ExpectedConditions.visibilityOf(driver.findElementByXPath("//android.widget.Button[@text='OK']")));
                WebElement clickOK = driver.findElementByXPath("//android.widget.Button[@text='OK']");
                clickOK.click();
                System.err.println("420;Download detailing file using WiFi connection;Application should be able to download the detailing file to the device;(Result=Failed!)");
            }
        }
        catch(NoSuchElementException e){

            wait.until(ExpectedConditions.visibilityOf(driver.findElementByXPath("//android.widget.Button[@text='YES']")));
            WebElement clickYes = driver.findElementByXPath("//android.widget.Button[@text='YES']");
            clickYes.click();
            wait.until(ExpectedConditions.visibilityOf(driver.findElementByXPath("//android.widget.Button[@text='OK']")));
            WebElement clickOK = driver.findElementByXPath("//android.widget.Button[@text='OK']");
            if(clickOK.getAttribute("text").contains("successfully")){
                clickOK.click();
                System.out.println("420;Download detailing file using WiFi connection;Application should be able to download the detailing file to the device;(Result=Passed!)");
            }
        }
    }

虽然我已将其设置为等待120秒,但我仍然会收到NoSuchElementException错误:

Exception in thread "AWT-EventQueue-0" org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 2.42 seconds

以下是appium日志:

> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: find
> info: [debug] [BOOTSTRAP] [debug] Finding //android.widget.Button[@text='OK'] using XPATH with the contextId:  multiple: false
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"Could not find an element using supplied strategy. ","status":7}
> info: [debug] Condition unmet after 2407ms. Timing out.
> info: [debug] Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.","origValue":"Could not find an element using supplied strategy. "},"sessionId":"147e3950-c6f9-4790-8f6f-e9ed70a9aaa9"}
> info: <-- POST /wd/hub/session/147e3950-c6f9-4790-8f6f-e9ed70a9aaa9/element 500 2419.570 ms - 230

我在ExpectedConditions下尝试了其他方法,例如elementToBeClickable等。还使用了Thread.sleep()。我仍然不知道是什么原因引起的。我的驱动程序超时是此错误的一个因素。如果我发布的代码不够,只需询问,我可以发布更多。提前谢谢!

1 个答案:

答案 0 :(得分:0)

等到应该成为元素初始化的一部分

WebElement clickOK=wait.until(ExpectedConditions.visibilityOf(driver.findElementByXPath("//android.widget.Button[@text='OK']")));