Python Selenium - 如何处理在WebDriverWait完成之后才显示的警报?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python Selenium - 如何处理在WebDriverWait完成之后才显示的警报?相关的知识,希望对你有一定的参考价值。
目前我正在尝试自动化订单流程,并且在某些时候,如果最近下了订单,会弹出一条警告,说明“客户已在2天内订购了这些相同的商品。请检查是否有重复或检查覆盖重复订单以继续。“
我已尝试以下拦截该警报并允许脚本继续运行:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support import expected_conditions as ExCon
from selenium.common.exceptions import TimeoutException, NoSuchElementException, NoAlertPresentException,
UnexpectedAlertPresentException
from oleaprocs import Order
"""
Will put the following constants in their own file later
"""
GEN_NEXT_ID = "cmdNext"
SHIP_DET_METH_ID = "cboAvailItemsShip"
SHIP_DET_NEXT_ID = "cmdNext"
ORDER_REP_ID = "cboServiceRep"
EMAIL_ID = "ctl00_ctl00_main_content_main_content_ctl00_txtEmail"
SEARCH_RESULT_ID = "ctl00_ctl00_main_content_main_content_ctl00_dgResults_ctl03_lnkCustName"
ADD_ORDER_ID = "cmdAddNewOrder"
LOGIN_ID = 'ctl00_ctl00_main_content_main_content_ctl00_UserName'
PASS_ID = 'ctl00_ctl00_main_content_main_content_ctl00_password'
SIGNIN_ID = 'ctl00_ctl00_main_content_main_content_ctl00_SigninBtn'
PLACED_VIA = 'Phone'
BILL_OPTION = 'Visa x1111'
username = 'user'
password = 'pass'
customer = 'user'
items = {'Item1': '100', 'Item2': '125'} # One or many items - also noting quantity
# This setup will likely be steamlined to determine whose server to use, etc
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("website-placeholder-for-security-reasons")
# Login will be separated out into a function, to allow for various logins
# log-in
driver.find_element_by_id(LOGIN_ID).send_keys(username)
driver.find_element_by_id(PASS_ID).send_keys(password)
driver.find_element_by_id(SIGNIN_ID).click()
driver.find_element_by_xpath('//a[text()="Customers"]').click()
# Type in search for user-email - separated to function later
search_email = driver.find_element_by_id(EMAIL_ID)
search_email.send_keys(customer)
search_email.send_keys(Keys.RETURN)
# Click the (hopefully) singular search result
driver.find_element_by_id(SEARCH_RESULT_ID).click()
#search_result = driver.find_element_by_id(SEARCH_RESULT_ID).click()
#search_result.click()
### Start creating new order ###
## Order Items ##
# Accept Customer Orders alert
"""
alert_obj = driver.switch_to.alert
alert_obj.accept()
"""
Alert(driver).accept()
# Click on "Add New Order"
driver.find_element_by_id(ADD_ORDER_ID).click()
order = Order(driver)
# Set Placed Via
placed_via = Select(driver.find_element_by_name('cboPlacedVia'))
placed_via.select_by_visible_text(PLACED_VIA) # could also set IDs in a dict
# Set Billing Option
bill_option = Select(driver.find_element_by_name('ctrlBillOption$cboBillOption'))
bill_option.select_by_visible_text(BILL_OPTION)
# Add items to order
order.add_items(items)
# Will likely do some assertions here at some point, for custom / printed items
driver.find_element_by_id('cmdNext').click()
## Shipping Details ##
# Select Shipping Method
# If similar order recently placed - confirm alert and try/except duplicate order override
# driver.implicitly_wait(2)
try:
print("WebDriverWait - 2 seconds")
WebDriverWait(driver, 2).until(ExCon.alert_is_present())
Alert(driver).accept()
print("Switching to Alert")
#alert_dupe = driver.switch_to.alert
# print(str(alert_dupe.text))
# alert_dupe.accept()
except TimeoutException:
print("Timeout")
except NoAlertPresentException:
print("No Alert Present")
except UnexpectedAlertPresentException:
print("Unexpected alert, yo!")
try:
driver.find_element_by_id('chkDuplicateOrderOverride').click()
except NoSuchElementException:
print("No duplicate order detected")
# Continue
driver.find_element_by_id('cmdNext').click()
## Order Group Details ##
# Check for Authorize button (I could bypass this when I implement "order_type" later
try:
driver.find_element_by_id('cmdAuthorize').click()
except NoSuchElementException:
print("No Authorize Element Found - This payment method does not require authorization")
driver.find_element_by_id('cmdPlaceOrder').click()
并引用了另一个文件:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
"""
Order will be used to keep track of the state of an order throughout
the order-process. This can include things like the order total, items
included, and can even get as complicated as 1:1 mirroring all of the
details of an order. This will depend on our requirements.
"""
class Order(object):
def __init__(self, driver):
self.driver = driver
# self.product_name_field = self.driver.find_element_by_id('txtProductName')
# self.quantity_field = self.driver.find_element_by_id('txtQuantity')
# self.add_item_button = self.driver.find_element_by_id('cmdAddSaveItem')
# Add items to an order
def add_items(self, items):
"""
Will need to make the quantity check in here.
"""
# product_name_field = self.driver.find_element_by_id('txtProductName')
# quantity_field = self.driver.find_element_by_id('txtQuantity')
# add_item_button = self.driver.find_element_by_id('cmdAddSaveItem')
"""
Cannot simply one-time set the above because of the references
going stale x_x - need to see if I can ignore stale-references.
"""
for item, quantity in items.items():
product_name_field = self.driver.find_element_by_id('txtProductName')
product_name_field.send_keys(item)
product_name_field.send_keys(Keys.TAB)
quantity_field = self.driver.find_element_by_id('txtQuantity')
quantity_field.send_keys(quantity)
quantity_field.send_keys(Keys.TAB)
add_item_button = self.driver.find_element_by_id('cmdAddSaveItem')
add_item_button.click()
我对之前的警报发表了评论,可能没有释放资源......我有一些早期的代码处理一个总是弹出的警报,所以我没有try
那一个:
alert_obj = driver.switch_to.alert
alert_obj.accept()
我在这里使用driver.switch_to.alert
变体因为我在之前的代码中尝试了两种变体而且都没有用。
基本上当代码到达WebDriverWait(drive, 2)
时会发生什么...是浏览器坐在那里等待2秒,然后警报弹出。我已经尝试了各种任意长度的时间,它会一直等待那段时间,然后显示警报。我觉得我在这里被拖走了:|
以下是发生这种情况后收到的错误信息:
Traceback (most recent call last):
File "C:UsersuserDocumentsProjectsSeleniumproj.py", line 136, in <module>
driver.find_element_by_id('cmdAuthorize').click()
File "C:Pythonlibsite-packagesseleniumwebdriver
emotewebdriver.py", line 353, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:Pythonlibsite-packagesseleniumwebdriver
emotewebdriver.py", line 957, in find_element
'value': value})['value']
File "C:Pythonlibsite-packagesseleniumwebdriver
emotewebdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "C:Pythonlibsite-packagesseleniumwebdriver
emoteerrorhandler.py", line 241, in check_response
raise exception_class(message, screen, stacktrace, alert_text)
selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None
Message: unexpected alert open: {Alert text : Customer has placed an order for these same items within 2 days. Please check for duplicates or check Override Duplicate Order to continue.}
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.16299 x86_64)
知道为什么会这样吗?如果我没记错的话,我不相信我们真的有任何警报也有复选框或输入,所以我不会100%反对只有100%阻止警报被Selenium确认(或自动接受它们),但如果可以,我想避免这样做。
如果最近没有下订单(或者我使用不同的帐户来测试一遍又一遍的订单创建),代码运行完全正常并且正确地下订单。
从添加项目(在有问题的页面之前)开始的手动步骤将是:
1)输入项目名称,然后输入项目数量2)对“项目”项目中的每个项目执行此操作3)点击下一个按钮4)下一页带有装运选项“装载”5)如果最近下订单,则会出现问题 - WebDriverWait似乎停止加载页面?看起来就是这种情况,因为警报在WebDriverWait时间用完之后才会显示。
在告诉Selenium点击下一个按钮进入下一页后,我并没有做任何特别的事情。与之前的“点击下一个按钮”操作没有什么不同,并且页面不应该与其他页面的行为有任何不同 - 即使警报显然只是没有它x_x
在警报检查之前我没有“点击下一步按钮” - 直到我首先尝试提交运送详细信息之后才会弹出警报。之所以在警报检查失败后似乎继续采取进一步措施的原因。
孩子们,事先概述了这个过程。
以上是关于Python Selenium - 如何处理在WebDriverWait完成之后才显示的警报?的主要内容,如果未能解决你的问题,请参考以下文章