简体   繁体   中英

Selenium Webdriver Java code to click on modal popup does work in debug mode only

I'm trying to click a button on a modal popup, but the code executes to the end only in debug mode (Eclipse Kepler). I tried to insert a ExplicitWaitTime method to wait some seconds after the popup is opened but still is not working. This is the code I am using:

package com.website.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.JavascriptExecutor;

public class CaseOne {
public static void main(String[] args) {
    System.setProperty("webdriver.firefox.profile", "SP");
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.website.test"); //step 1

    WebElement searchFirstProduct = driver.findElement(By.cssSelector(".sortResultProdName")); //step 2: locate the Webelement
    searchFirstProduct.click(); //step 2: click on the first product found 

    WebElement AddtoCart = driver.findElement(By.id("pdAddToCart")); //step 3
    AddtoCart.click();// step 3: click on the add to cart button

   // -----------------BEGIN CODE TO FIND THE POPUP WINDOW AND SWITCH TO IT -------------------------
    WebElement ModalForm = driver.findElement(By.id("add-to-cart"));//find element add-to-cart which contains the iframe
    WebElement k = ModalForm.findElement(By.tagName("iframe"));

    driver.switchTo().frame(k);
    WebElement n = driver.findElement(By.tagName("form"));

   //------------------END CODE TO FIND THE POPUP WINDOW AND SWITCH TO IT -----------------------------

    WebElement checkOut = n.findElement(By.id("aCheckOut1"));
    checkOut.click();

}  

}

Basically it works until the execution reaches

 WebElement k = ModalForm.findElement(By.tagName("iframe"));

then nothing happens, but if I run the code in debug and I put a breakpoint on the last three lines of code

WebElement n = driver.findElement(By.tagName("form"));
WebElement checkOut = n.findElement(By.id("aCheckOut1"));
checkOut.click();

and execute them one at a time, it works. It appears to me that it might need to wait some time between finding the popup button and click on it. Any suggestion?

Thank you. Any suggestion? Thanks.

You may need to wait for the iframe to be ready.

Try with frameToBeAvailableAndSwitchToIt :

long defaultTimeOutInSecs = 10;
By iframeLocator = By.cssSelector("#add-to-cart iframe");
new WebDriverWait(driver, defaultTimeOutInSecs)
  .until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(iframeLocator));

instead of:

WebElement ModalForm = driver.findElement(By.id("add-to-cart"));//find element add-to-cart which contains the iframe
WebElement k = ModalForm.findElement(By.tagName("iframe"));
driver.switchTo().frame(k);

Also, as soon as you finish working in the iframe space, you should switch back to the original document with TargetLocator#defaultContent , like this:

driver.switchTo().defaultContent();
WebElement checkOut = n.findElement(By.id("aCheckOut1"));
[..]

SO at the end, after trying WebDriverWait in any fashion available, I was able to pause the code using a thread sleep like this:

try {
    Thread.sleep(2000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Not sure why sometime I can use successfully WebDriverWait and some other times I can't. If I will find out, I will post it here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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