简体   繁体   中英

Issue with Selenium - Pop Up window

有时有弹出窗口但有时不出现时如何放置条件。

    try{  
        WebDriverWait wait = new WebDriverWait(driver, 5);
        wait.until(ExpectedConditions.alertIsPresent());
        System.out.println(driver.switchTo().alert().getText());
        driver.switchTo().alert();  
    }     
    catch (NoAlertPresentException Ex)  
    {
        System.out.println("No alert");
    }

try the following

from selenium import webdriver
from selenium.common.exceptions import NoAlertPresentException


def example():
    firefox_browser = webdriver.Firefox(executable_path=r'geckodriver.exe')
    firefox_browser.get('https://www.google.com')

    """check if alert is displayed
    """
    try:
        alert = firefox_browser.switch_to.alert
    except NoAlertPresentException:
        print("No alert")
        return False
    else:
        return True


if __name__ == "__main__":
    example()

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