无法在Selenium中的模式弹出窗口上找到元素 [英] Not able to locate element on a modal pop up window in Selenium

查看:620
本文介绍了无法在Selenium中的模式弹出窗口上找到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium Webdriver自动化网页.我无法使用简单元素定位器方法在模式弹出窗口中单击按钮.

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.

示例:

  • 打开www.walmart.com
  • 在搜索框中输入电视.
  • 选择一些电视,然后单击添加到购物车"
  • 现在出现了一个弹出窗口,其中有签出"按钮.我需要点击此结帐"按钮.

我尝试了switchTo()窗口句柄,尝试了switchTo()框架,但没有任何效果.

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

推荐答案

该网站运行缓慢,存在加载问题.因此,我建议您对每个findElement使用显式等待.我编写了以下脚本,效果很好

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

这篇关于无法在Selenium中的模式弹出窗口上找到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆