简体   繁体   中英

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 :

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 :

<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:

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:

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. 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. To check if it's in an IFRAME, you would need to inspect the dialog and then move up the DOM. If you reach the top of the DOM without passing through an IFRAME, then your dialog is not in an IFRAME.

You should be able to click either of those buttons with a simple XPath, depending on which one you want to click on:

//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:

  • css :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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