简体   繁体   English

无法在Selenium WebDriver中处理Ajax弹出窗口

[英]Unable to Handle Ajax Pop up in Selenium WebDriver

I'm new in Selenium. 我是Selenium的新人。 I'm trying to make Script in selenium of MacDonald's login and Order.But After login I'm unable to click Continue and any other option please help me. 我正在尝试在MacDonald登录和订单的selenium中创建脚本。但是在登录后我无法点击继续,任何其他选项请帮助我。

my code is:-- 我的代码是: -

WebDriver selenium= new FirefoxDriver();
selenium.manage().window().maximize();

String baseurl = "http://www.mcdelivery.co.in/";
selenium.get(baseurl);

Thread.sleep(2000); 
Thread.sleep(5000);

WebElement loginbtn = selenium.findElement(By.id("lnkBtnLogin"));
if(loginbtn.isDisplayed()) {
    loginbtn.click();
}

WebElement username = selenium.findElement(By.id("txtMobileNumber"));
if(username.isDisplayed()) {
    username.clear();
    username.sendKeys("******");
}

WebElement pwd = selenium.findElement(By.id("txtMsgPwd"));
if(pwd.isDisplayed()) {
    pwd.sendKeys("******");
}

WebElement submit = selenium.findElement(By.id("btnSubmit"));
if(submit.isDisplayed()) {
    submit.click(); 
    //Alert aler = selenium.switchTo().alert().accept();
    selenium.switchTo().activeElement();         
}

WebElement conti = selenium.findElement(By.id("btnContinue"));
if(conti.isDisplayed()) {
    conti.click();
}

// selenium.close();

After running this I'm Getting Error 运行此后我得到错误

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: 
{"method":"id","selector":"btnContinue"}
Command duration or timeout: 31 milliseconds

Since the Continue button is the part of the pop-up (which takes time to load), you need to give some time to webdriver to detect it. 由于“ 继续”按钮是弹出窗口的一部分(需要一些时间来加载),因此您需要花一些时间让webdriver检测它。 For that purpose, give a timeout using Implicit/Explicit waits . 为此,使用Implicit / Explicit等待给出超时。

For above, you can use an Implicit timeout at the top like this; 对于上面,你可以像这样使用顶部的隐式超时 ;

selenium.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

The above gives a 15 seconds time to the webdriver, each time it tries to locate an element. 每次尝试定位元素时,上面给webdriver一个15秒的时间。

Also, in this code, 此外,在此代码中,

if(conti.isDisplayed())

{ 

conti.click();

}

for some odd reasons, the Continue button wasn't getting clicked when inside the if ( conti.isDisplayed() ) . 由于某些奇怪的原因,在if(conti.isDisplayed())内部没有点击继续”按钮 So, lose that loop. 所以,失去那个循环。 Just use conti.click(); 只需使用conti.click(); . It worked when tried! 它试用了!

Hi use some kind of explicit wait after switching to the popup like 你好切换到弹出窗口之后使用某种显式等待

         boolean ajaxPopup = new WebDriverWait(driver,10).until(ExpectedConditions
                 .visibilityOfElementLocated(By.id("btnContinue"))) != null;

         Assert.assertTrue(ajaxPopup);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何处理linkedIn授权使用Java在Selenium Webdriver中弹出? - How to Handle linkedIn authorization Pop up in selenium webdriver using Java? 如何使用Java处理Selenium WebDriver中的弹出窗口 - How to handle Pop-up in Selenium WebDriver using Java 如何处理硒webdriver中具有空白ID的弹出窗口? - How to handle pop up windows with blank id in selenium webdriver? 无法在Selenium Webdriver中处理Sharepoint弹出页面 - Cannot handle Sharepoint pop-up page in Selenium Webdriver 如何在 selenium webdriver 中处理 Javascript 警报/弹出窗口 - How to handle Javascript Alert/pop up window in selenium webdriver 无法使用Java在Selenium Webdriver中的弹出窗口中单击按钮 - Unable to click on a button in Pop-up in Selenium webdriver using java 如何处理硒Webdriver中的简单HTML弹出窗口? - How to handle a pop up which is a simple HTML pop up in selenium webdriver? 处理硒中弹出的窗口 - Handle Window Pop Up in Selenium 如何使用Java处理Selenium Webdriver中下一个选项卡中弹出的所需身份验证? - How to handle authentication required pop up in next tab in selenium webdriver using java? 使用Selenium Webdriver下载excel时如何处理firefox中的下载弹出窗口 - How to handle download pop-up in firefox, while downloading excel using Selenium Webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM