在for循环中使用selenium函数时出错

问题描述 投票:0回答:1

我目前正在使用我公司的票务网站进行测试,因此当我通过在

incident_ticket
中添加票号来运行此代码时,一切都很顺利,但由于我想对许多票进行批量自动操作,所以在尝试时会遇到随机错误使用列表在 for 循环中执行此操作
incidents_list

当我执行代码时,列表的第一项

incidents_list
被正确处理,但接下来的项目会导致以下错误:

An error occurred while processing ticket IN6519342: Message: element click intercepted: Element <img id="m45bfddac-img" sf="1" ctype="image" alt="Seleccionar valor" src="img_lookup.gif" source="img_lookup" imgtype=".gif" style="cursor: default; display: inline; margin: 0px;" border="0" lc="m45bfddac-tb" aria-haspopup="true" class="dButton " align="center" ev="Valuelist" tabindex="-1" title="Seleccionar valor" draggable="false" clicked="true" changed_by_user="false"> is not clickable at point (335, 102). Other element would receive the click: <div id="wait" width="100%" height="100%" class="wait" style="position: absolute; top: 0px; left: 0px; height: 945px; width: 1928px; display: block; cursor: wait;">...</div>
  (Session info: chrome=130.0.6723.69)

这是代码,我目前正在学习 Selenium,因此任何帮助都会得到赞赏:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

email = '[email protected]'
pwd = 'password123'

incidents_list = ["inc_0001", "inc_0002", "inc_0003", "inc_0004", "inc_0005", "inc_0006", "inc_0007", "inc_0008", "inc_0009"]

incident_ticket = 'inc_0010'
log_title = 'Note'
log_text = "Ticket it's beign resolved as low priority case, approved by PM."

timeout = 20


def ticket_processing(driver, incident):
    try:
        # WebDriverWait(driver, timeout).until(EC.invisibility_of_element((By.ID, "msgbox-dialog_inner_dialogwait")))
        quicksearch_id = WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.ID, "quicksearch")))
        # driver.execute_script("arguments[0].scrollIntoView();", quicksearch_id)
        quicksearch_id.clear()
        quicksearch_id.send_keys(incident)
        quicksearch_id.send_keys(Keys.ENTER)

        WebDriverWait(driver, timeout).until(EC.invisibility_of_element((By.ID, "msgbox-dialog_inner_dialogwait")))
        urgency_id = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.ID, "m45bfddac-img")))
        # driver.execute_script("arguments[0].scrollIntoView();", urgency_id)
        WebDriverWait(driver, timeout).until(EC.invisibility_of_element((By.ID, "msgbox-dialog_inner_dialogwait")))
        urgency_id.click()
        p3_selection_id = WebDriverWait(driver, timeout).until(
            EC.element_to_be_clickable((By.ID, "lookup_page1_tdrow_[C:1]_ttxt-lb[R:2]")))
        p3_selection_id.click()

        new_log_btn_id = WebDriverWait(driver, timeout).until(
            EC.element_to_be_clickable((By.ID, "ma877f29e_bg_button_addrow-pb")))
        # driver.execute_script("arguments[0].scrollIntoView();", new_log_btn_id)
        new_log_btn_id.click()
        new_log_title_id = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.ID, "m8b7047bb-tb")))
        new_log_title_id.send_keys(log_title)
        new_log_text_id = WebDriverWait(driver, timeout).until(
            EC.element_to_be_clickable((By.ID, "mfc77772d-rte_iframe")))
        new_log_text_id.send_keys(log_text)

        save_btn_id = WebDriverWait(driver, timeout).until(
            EC.element_to_be_clickable((By.ID, "toolactions_SAVE-tbb_image")))
        save_btn_id.click()
        print(f"Ticket {incident} processed successfully.")
    except Exception as err:
        print(f"An error occurred while processing ticket {incident}: {err}")


def main():
    chrome_options = Options()
    chrome_options.add_argument("--disable-extensions")
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("--window-size=1920,1080")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--headless=new")
    chrome_options.add_argument("--disable-dev-shm-usage")

    driver = webdriver.Chrome(options=chrome_options)

    driver.get("https://www.company.website.com")
    id_element = driver.find_element(By.ID, "j_username")
    id_element.send_keys(email)
    pwd_element = driver.find_element(By.ID, "j_password")
    pwd_element.send_keys(pwd)
    pwd_element.send_keys(Keys.ENTER)
    incident_id = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.ID, "FavoriteApp_INCIDENT")))
    incident_id.click()

    # function without for loop, everything goes fine
    # ticket_processing(driver, incident_ticket)

    # this is were I ge the errors
    for incident in incidents_list:
        ticket_processing(driver, incident)

if __name__ == '__main__':
    main()

谢谢:D

python selenium-webdriver web automation
1个回答
0
投票

尝试重新排序步骤:

    WebDriverWait(driver, timeout).until(EC.invisibility_of_element((By.ID, "msgbox-dialog_inner_dialogwait")))
    urgency_id = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.ID, "m45bfddac-img")))
    urgency_id.click()
© www.soinside.com 2019 - 2024. All rights reserved.