Daniel
Daniel

Reputation: 64

driver does not wait for element to be visible

Im having this problem in Appium where the AndroidDriver does not wait for the OK button(android.widget.Button[@text='OK']) in the catch block to appear. Here is the code:

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!)");
            }
        }
    }

Although I have set it to wait for 120 seconds, I still get this NoSuchElementException error:

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

Here is the appium logs:

> 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

I have tried other methods under ExpectedConditions such as elementToBeClickable and the likes. Also used Thread.sleep(). I still don't know what causes this. Is my driver timeout a factor to this error. If the codes I have posted is not enough just ask and I can post more. Thanks in advance!

Upvotes: 1

Views: 5706

Answers (1)

karthick23
karthick23

Reputation: 1331

wait until should be a part of element initialization

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

Upvotes: 2

Related Questions