Python在表单上迭代

2024-06-06 00:32:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用数据列表自动执行表单断言,但是在何时以及如何使用“WebDriverWait”或driver implict wait上,我遇到了困难。我的单子是1000串。当我运行一个100的样本时,只有不到100个被正确捕获。下面的代码捕获ElementNotSelectableException\StaleElementReferenceException,但没有正确寻址

import unittest

from selenium.common.exceptions import ElementNotVisibleException, ElementNotSelectableException, \
    ElementNotInteractableException, StaleElementReferenceException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome()

oar_order_id = '#oar-order-id'
oar_zip_code = '#oar_zip'
#order confirmation list
order_id_list = ["WO-34284975","WO-50002804","WO-50004302","WO-34282964","WO-34214963"]

#zip code confirmation list
zip_code_list = ["23508","99338","62036","75074-7520","37763","98034","89406-4361"]

submit_button = 'body.sales-guest-form.page-layout-1column:nth-child(2) div.page-wrapper:nth-child(10) main.page-main:nth-child(4) div.columns:nth-child(4) div.column.main form.form.form-orders-search:nth-child(4) div.actions-toolbar div.primary > button.action.submit.primary'
# Enter orderid & Zip & click enter

found = []
notfound = []
length = len(order_id_list)
driver.implicitly_wait(10)
wait = WebDriverWait(driver, 15, poll_frequency=1,
                     ignored_exceptions=[ElementNotVisibleException ,ElementNotSelectableException])
driver.get('https:/www.ecklers.com/sales/orderlookup/index/')
# Sample of 10 from list
for i in range(10):
    try:
        wait
        element1 = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, oar_order_id)))
        element1.send_keys(order_id_list[i])

        element2 = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, oar_zip_code)))
        element2.send_keys(zip_code_list[i])

        element3 = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, submit_button)))
        wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, submit_button)))
        element3.click()
        wait
        wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"body.sales-guest-view.page-layout-1column:nth-child(2) div.page-wrapper:nth-child(10) main.page-main:nth-child(4) div.page-title-wrapper:nth-child(3) h1.page-title > span.base")))
        title = driver.title
        #driver.implicitly_wait(60)

    except(StaleElementReferenceException):
        print("StaleElementReferenceException")

    except(ElementNotInteractableException):
        print("ElementNotInteractableException")

    try:
        text = order_id_list[i]
        assert(title.endswith(text[3:]))
        found.append(text)
    except(AssertionError):
        notfound.append((order_id_list[i]))
    driver.get('https:/www.ecklers.com/sales/orderlookup/index/')
    wait
print(found,notfound)```

Tags: fromimportdividchildbydriverpage