简体   繁体   English

处理硒中的警报

[英]Handle alerts in selenium

I am trying to handle an alert or a frame, I am not exactly sure of if it is an alert or a frame. 我正在尝试处理警报或框架,我不确定是警报还是框架。

I logged into instagram using selenium using below code : 我使用以下代码使用硒登录到instagram:

driver.get('https://www.instagram.com/accounts/login/');
var username = driver.findElement(By.name("username", 10000));
username.sendKeys("username");
var password = driver.findElement(By.name("password", 10000));
password.sendKeys("password");
var login = driver.findElement(By.xpath("//button[@type='submit']", 5000));
login.click();

Now after this executes and I am logged in I see below : 现在执行此操作并登录后,我将看到以下内容:

alert or Frame 警报或框架

I am doing below do dismiss this alert 我在下面做不要关闭此警报

 function dismissAlert(){
    var alert = driver.switchTo().alert();
    console.log(alert.getText());
    alert.dismiss();
}

But not able to dismiss this alert and go back to main window. 但是无法消除此警报并返回主窗口。 What is wrong I am doing here. 我在这里做什么错。

HTML CODE FOR ABOVE SS : 高于SS的HTML代码:

<div class="pbNvD fPMEg " role="dialog">
    <div class="piCib">
        <div class="dsJ8D">
            <div class="xlTJg">
                <div class="G3yoz">
                    <img height="76px" width="76px" src="/static/images/ico/xxhdpi_launcher.png/99cf3909d459.png" alt="">
                </div>
            </div>
        </div>
        <div class="_08v79">
            <h2 class="_7UhW9 x-6xq yUEEX KV-D4 uL8Hv">Turn on Notifications</h2>
            <div class="_7UhW9 xLCgt MMzan _0PwGv uL8Hv">Get notifications when you have new followers, likes or comments you may have missed.</div>
        </div>
        <div class="mt3GC">
            <button class="aOOlW bIiDR" tabindex="0">Turn On</button>
            <button class="aOOlW HoLwm" tabindex="0">Not Now</button>
        </div>
    </div>
</div>

Using javascript it could be like: 使用javascript可能像:

JavascriptExecutor js;
if (driver instanceof JavascriptExecutor) {
    js = (JavascriptExecutor)driver;
}
...
js.executeScript("document.querySelector('button.aOOlW.HoLwm').click()");
//Or
js.executeScript("document.querySelector('[role=\"dialog\"] button:nth-child(2)').click()");

Using java it could be like: 使用java可能像这样:

driver.findElement(By.cssSelector(".aOOlW.HoLwm", 10000)).click();
//Or
driver.findElement(By.xpath("/div[@role='dialog']//button[.='Not Now']")).click();

Wait a while for the element to be visible : 等待一段时间,该元素将可见:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement dialog = wait.until(
    ExpectedConditions.visibilityOfElementLocated(By.xpath("/div[@role='dialog']//button[.='Not Now']")));

dialog.click();

That's an HTML dialog, not an alert. 那是一个HTML对话框,而不是警报。 You can tell by right-clicking on the dialog. 您可以通过右键单击对话框来判断。 If you get a context menu and can inspect the HTML, you know that it's not an alert. 如果获得上下文菜单并可以检查HTML,则说明它不是警报。 To check if it's in an IFRAME, you would need to inspect the dialog and then move up the DOM. 要检查它是否在IFRAME中,您需要检查对话框,然后向上移动DOM。 If you reach the top of the DOM without passing through an IFRAME, then your dialog is not in an IFRAME. 如果您未通过IFRAME而到达DOM的顶部,则您的对话框不在IFRAME中。

You should be able to click either of those buttons with a simple XPath, depending on which one you want to click on: 您应该能够使用简单的XPath单击这两个按钮之一,具体取决于您要单击的按钮:

//button[.='Turn On']
//button[.='Not Now']

I would suggest that you add a brief wait for the element to be clickable since, at times, the dialog may take a moment to launch and finish loading. 我建议您添加一个简短的等待元素可单击,因为有时对话框可能需要一段时间才能启动并完成加载。

To dismiss the notification you need to click on the <button> with text as Not Now which is neither an Alert nor within any iframe and you can use either of the following solutions: 关闭通知,您需要单击<button> ,其文本为Not Now ,既不是警报,也不是任何iframe,并且可以使用以下任一解决方案:

  • css : css

     var login = driver.findElement(By.css("div[role='dialog'] button:nth-of-type(2)", 5000)); login.click(); 
  • xpath : xpath

     var login = driver.findElement(By.xpath("//div[@role='dialog']//following::button[2]", 5000)); login.click(); 

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

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