Not able to locate element on a modal pop up window in Selenium [closed]

删除回忆录丶 提交于 2019-12-02 07:49:29

问题


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();


来源:https://stackoverflow.com/questions/32685447/not-able-to-locate-element-on-a-modal-pop-up-window-in-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!