【发布时间】:2021-09-01 11:44:15
【问题描述】:
我正在使用 selenium 进行一些自动化工作。抱歉描述太长了。我是 Python 新手。 基本上有学生成绩门户。我们需要输入一个座位号,然后单击确定按钮查看结果。单击提交时会打开新窗口,其中使用 html 中的表格显示结果。
- 如果座位号无效,警报框会打开,指示座位号无效,然后关闭 ok 选项。
问题:
- 我想遍历从 1500 到 1600 的卷号。如果 1501 卷号无效,则会显示警报框。我想关闭警报框并继续使用第 1502 卷。
如果结果的值超过 96%,我想将计数增加 1。 2. 计算完结果打开后,我想关闭新打开的窗口,重新输入下一个座位号。继续计算
这是我的代码:
from selenium import webdriver
from selenium.common.exceptions import UnexpectedAlertPresentException
from selenium.webdriver.common.alert import Alert
import time
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
web = webdriver.Chrome(options=options,executable_path='J:\stuff\soft\chromedriver.exe')
web.get('https://msbte.org.in/DISRESLIVE2021CRSLDSEP/frmALYSUM21PBDisplay.aspx')
# variable to store the result
resultCount = 0
rlstart = 156857
rlend = 157299
try:
web.implicitly_wait(5)
pdl = web.current_window_handle
for x in range(rlstart, rlend):
web.implicitly_wait(1)
inp = web.find_element_by_xpath('//*[@id="txtEnrollSeatNo"]')
inp.send_keys(x)
submit = web.find_element_by_xpath('//*[@id="btnSubmit"]')
submit.click()
web.implicitly_wait(2)
web.implicitly_wait(2)
# pdl = web.current_window_handle
handles = web.window_handles
for handle in handles:
if(handle != pdl):
switch_to_alert().accept()
web.switch_to.window(handle)
getresult = web.find_element_by_css_selector('body > div > div:nth-child(3) > div:nth-child(4) > table > tbody > tr:nth-child(5) > td:nth-child(3) > strong').text
if(getresult > 96.00):
resultCount += 1
web.close()
web.switch_to.window(pdl)
web.implicitly_wait(2)
except UnexpectedAlertPresentException:
alert_obj = web.switch_to.alert
alert_obj.accept()
finally:
print("end")
web.quit()
print(resultCount)
这是错误
【问题讨论】:
标签: python selenium exception automation