2015-10-05 100 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' –