如何在 python selenium 的警告框中单击“确定”

how to click Ok in in alert box in python selenium

所以我在 python 中使用 selenium。我正在自动化 3 个网页。在我的第一页中,如果手机号码已填满,我的第二页会给我一个模态错误:This Mobile No is already attached to a submitted feedback。现在因为这个我的硒程序刚刚停止。我想在模式中单击“确定”并接受它,然后关闭浏览器 window,然后重新启动我的程序。我该怎么做?

错误window

我的代码

from sre_parse import State
from tkinter.tix import Select
from unicodedata import name
from selenium import webdriver

from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.alert import Alert

import pandas as pd
import xlrd
import time

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)







# readign the excel file
df  = pd.read_excel('New1Rajnandgaon_List.xlsx')

# looping through all the data

for i in df.index:
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), chrome_options=chrome_options)

    alert = Alert(driver)

    time.sleep(3)

    driver.get("https://ssg2021.in/citizenfeedback")

    time.sleep(3)
    # selcting states
    state_select = driver.find_element(By.XPATH,'//*[@id="State"]')
    drp1 = Select(state_select)

    drp1.select_by_visible_text('Chhattisgarh')

    time.sleep(2)


    # selecting district
    district_select = driver.find_element(By.XPATH,'//*[@id="District"]')
    drp2 = Select(district_select)

    drp2.select_by_visible_text('RAJNANDGAON')
    entry = df.loc[i]

    # entering the age
    age = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[1]/form/div[4]/div[1]/div/div/input')

    age.send_keys(str(entry['age']))

    # respondant name
    rs_name = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[1]/form/div[4]/div[2]/div/div[1]/div/input')
    rs_name.send_keys(entry['name'])

    # rs mobile number
    rs_number = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[1]/form/div[4]/div[2]/div/div[2]/div/input')
    rs_number.send_keys(str(entry['mobile number']))

    # rs gender
    gender = driver.find_element(By.XPATH,'//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[1]/form/div[4]/div[2]/div/div[3]/div/select')
    rs_gender = Select(gender)
    rs_gender.select_by_visible_text('Male')



    # submitting the form
    submit = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[1]/form/div[5]/input')
    submit.click()


    time.sleep(2)


    # second page
    # radio button 1
    radio_1 = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[2]/form/div[1]/div[1]/div[2]/div[1]/label[1]/input')
    driver.execute_script("arguments[0].click();", radio_1)
    radio_1.click()


    # radio button 2
    radio_2 = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[2]/form/div[1]/div[2]/div[2]/div[1]/label[1]')
    radio_2.click()


    # radio button 3
    radio_3 = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[2]/form/div[1]/div[3]/div[2]/div[1]/label[1]')
    radio_3.click()

    # radio button 4
    radio_4 = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[2]/form/div[1]/div[4]/div[2]/div[1]/label[1]')
    radio_4.click()


    # radio button 5
    radio_5 = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[2]/form/div[1]/div[5]/div[2]/div[1]/label[1]')
    radio_5.click()

    submit2 = driver.find_element(By.XPATH, '//*[@id="zed_user_form"]/div/div[1]/div[2]/div/div/div[2]/form/div[2]/input')
    submit2.click()

    # alert

    time.sleep(1)

    driver.close()

您需要切换到此警报并按以下方式接受它:

WebDriverWait(driver, 20).until(EC.alert_is_present())
alert = driver.switch_to.alert
alert.accept()