我正在使用selenium进行一些自动化工作。对不起,描述得太长了。我是Python新手。 基本上有学生结果门户。我们需要输入一个座位号,然后点击OK按钮查看结果。单击submit(提交)按钮,将打开一个新窗口,其中使用html格式的表格显示结果
问题:
如果结果值大于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)
这是错误
需要记录的事项:
time.sleep()
,下面的代码我把sleep放在这里只是为了视觉目的李>implicit
与explicit
混合使用李>rlend
的值,为了测试的目的,您必须增加它,看看它是否有效李>代码:-
导入:
您可以浏览下面的代码一次
我没有编辑你的代码,但它符合你的要求
while rlstart != rlend+1:
,rlend+1
因为如果有一个增量,156860
变成156861
,当rlstart
是156861
时,它会出现while循环,不会给出156861's
结果输出:
您的代码有几个问题
web.implicitly_wait(1)
不会在代码中插入实际的暂停。它只是设置超时。等待元素出现在页面上的时间。所以当你定义它两次这不会给您4秒的暂停时间,只定义2秒的超时时间两次,但不会暂停程序流。
此外,您不需要多次定义它,只需定义一次就可以了。
此外,我们通常将超时定义为10-20-30秒,而不是1-2秒。如果互联网连接速度慢/网站响应速度慢等,这可能会导致测试失败
因此,当座椅正确时
switch_to_alert().accept()
将失败-这是实际发生的情况,因为没有出现警报。我正在努力制作一个正确的代码,但是其他人给了你工作代码。因此,您可以阅读此处的说明和此处的工作代码:)
相关问题 更多 >
编程相关推荐