2015-10-05 119 views
2

嘗試點擊元素,但總是有一個UnexpectedAlertPresentException.Adding UnexpectedAlertPresentException給代碼不要chage任何東西,因爲元素沒有被點擊和彈出不appeard.I想可能是它的原因彈出沒有太多時間出現,但它也沒有幫助。 事件試着接受通過能力都彈出...無法點擊元素由於彈出

cap = DesiredCapabilities.FIREFOX 
cap["UnexpectedAlertPresentException"] = "accept" 
fox = webdriver.Firefox(capabilities=cap) 

fox.find_element_by_link_text("My Account").click() 
try: 
    WebDriverWait(fox, 3).until(EC.alert_is_present(), 
           'Timed out waiting for PA creation ' + 
           'confirmation popup to appear.') 
    fox.switch_to.alert().accept() 

    print "alert accepted" 
except TimeoutException: 
    print "no alert" 

回答

1

您可以嘗試..

try { 
    WebDriverWait wait = new WebDriverWait(driver, 2); 
    wait.until(ExpectedConditions.alertIsPresent()); 
    Alert alert = driver.switchTo().alert(); 
    alert.accept(); 
} catch (Exception e) { 
    //exception handling 
} 
+0

我有python 2.7 –

1

硒2 switchin警報改變。更改您的命令fox.switchTo().alert().accept();

+0

我試試這個,它沒有改變任何東西,仍然有UnexpectedAlertPresentException。 –

+0

放'進口時間; time.sleep(2);'彈出前檢查它是否打開調試信息 –

+0

AttributeError發生了另一個問題:'WebDriver'對象沒有屬性'switchTo' –