【问题标题】:how to click Ok in in alert box in python selenium如何在 python selenium 的警报框中单击确定
【发布时间】:2022-01-20 07:40:51
【问题描述】:

所以我在 python 中使用 selenium。我正在自动化 3 个网页。在我的第一页中,如果手机号码已填写,我的第二页会以模式显示此错误:此手机号码已附加到提交的反馈中。现在正因为如此,我的硒程序刚刚停止。我想在模式中单击“确定”并接受它,然后关闭浏览器窗口,然后重新启动我的程序。我该怎么做?

错误窗口

我的代码

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()

【问题讨论】:

    标签: python excel pandas selenium webautomation


    【解决方案1】:

    您需要切换到此警报并接受它,如下所示:

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

    【讨论】:

    • 工作!接受答案(:
    • 我明白了 ;) 永远欢迎你 :)
    猜你喜欢
    • 2020-09-03
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    相关资源
    最近更新 更多