Not able to locate element on a modal pop up windo

2019-03-01 02:33发布

问题:

I am automating a webpage using Selenium Webdriver. I am not able to click a button a modal pop up window using simple element locator method.

Example:

  • open www.walmart.com
  • enter tv in the search box.
  • select some tv and click "Add to Cart"
  • Now a pop up window comes where "Checkout" button is located. I need to click on this "checkout" button.

I tried switchTo() windowhandle, I tried switchTo() frame but nothing worked.

回答1:

This website is very slow and has loading issue. So, I suggest you to use Explicit wait for each findElement. I have written the following script and works perfectly

WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();        

driver.get("http://www.walmart.com/");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("[placeholder='Search']")))
        .sendKeys("TV");
driver.findElement(By.cssSelector(".searchbar-submit.js-searchbar-submit")).click();
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("#tile-container>div>a>img")))
        .get(0).click();        
wait.until(ExpectedConditions.elementToBeClickable(By.id("WMItemAddToCartBtn"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.id("PACCheckoutBtn"))).click();