使用Selenium python自动关闭对话框

问题描述 投票:0回答:1

使用Selenium的程序

from selenium import webdriver
browser = webdriver.Firefox()
url = 'any_url'
browser.get(url)
browser.find_element_by_id('empty_cart_btn').click()

创建以下对话框:

Selenium中的代码是什么自动按OK?

python selenium selenium-webdriver
1个回答
3
投票

试试accept()selenium.webdriver.common.alert

未经测试的代码如下:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
url = 'any_url'
browser.get(url)
browser.find_element_by_id('empty_cart_btn').click()

try:
    WebDriverWait(browser, 5).until(EC.alert_is_present(), 'Waiting for alert timed out')

    alert = browser.switch_to_alert()
    alert.accept()
    print "alert accepted"

except TimeoutException:
    print "no alert"
© www.soinside.com 2019 - 2024. All rights reserved.