[Python] Failed to locate the dynamic element

Discussão em 'Python' iniciado por Stack, Fevereiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    problem description


    When I was writing an automated test script, I was able to locate the element with F12, but the script didn't click on the element at all.
    I have tried many ways to solve this problem.

    Methods that have been tried


    Here's what I tried.
    I've wrapped two methods to explicitly wait for an element to appear in the code.But it doesn't make effect.
    `

    basepage
    def wait_for_element(self, by, value, timeout=60):
    """等待元素出现并且可见"""
    try:

    element = WebDriverWait(self.w, timeout).until(

    EC.presence_of_element_located((by, value))
    )
    element = WebDriverWait(self.w, timeout).until(
    EC.visibility_of_element_located((by, value))
    )
    return element
    except TimeoutException:
    logger.error(f"Timeout waiting for element {value} to appear.")
    raise Exception(f"Element {value} was not present within {timeout} seconds.")
    # return WebDriverWait(self.w, timeout).until(
    # EC.presence_of_element_located((by, value))
    # )`



    `

    basepage
    def wait_for_element_and_click(self, by, value, timeout=30):
    try:
    # 等待元素可点击
    element = WebDriverWait(self.w, timeout).until(
    EC.element_to_be_clickable((by, value))
    )
    if not element.is_enabled():
    raise Exception(f"Element {value} is disabled.")
    # 确保没有其他元素遮挡
    WebDriverWait(self, timeout).until(
    lambda d: not self.is_element_obstructed(element)
    )
    # 执行点击操作
    element.click()
    except TimeoutException:
    logger.error(f"Timeout waiting for element {value} to be clickable.")
    raise Exception(f"Element {value} was not clickable within {timeout} seconds.")
    # 元素是否有遮挡



    page
    select_action = PageElement(xpath="(//div[@class='ant-select-selector'])[3]")
    def locate_action(self, text):
    try:
    self.wait_for_element(*self.add_action)
    self.wait_for_element_and_click(*self.add_action)
    self.select_action.click()
    # self.wait_for_element(*self.select_action)
    # self.scroll_to_element(*self.select_action)
    # self.click_element_by_js(*self.select_action)
    # self.wait_for_element_and_click(*self.select_action)
    # self.select_action.click()
    self.wait_for_element(*self.alert_dialog)
    self.wait_for_element_and_click(*self.alert_dialog)
    self.wait_for_element(*self.select_time)
    self.wait_for_element_and_click(*self.select_time)
    self.wait_for_element(*self.select_time_value)
    self.wait_for_element_and_click(*self.select_time_value)
    self.select_input.send_keys(text)
    except TimeoutException as e:
    logger.error(f"TimeoutException occurred: {e}")
    raise
    except NoSuchElementException as e:
    logger.error(f"NoSuchElementException occurred: {e}")
    raise
    except Exception as e:
    logger.error(f"Unexpected error occurred during setting custom condition: {e}")
    raise
    `
    Remark:The problem is in this line of code:self.select_action.click().
    # Here's the error.
    `ERROR:pages.base_page:Element not found: ('xpath', "(//div[@class='ant-select-selector'])[3]")
    ERROR:pages.base_page:TimeoutException occurred: Message:
    Stacktrace:
    GetHandleVerifier [0x00007FF722DD6F15+28773]

    GetHandleVerifier [0x00007FF72313147D+3544525]
    GetHandleVerifier [0x00007FF722E9C9DA+838442]
    (No symbol) [0x00007FF722D42600]
    (No symbol) [0x00007FF722BD8FAA]
    (No symbol) [0x00007FF722C2F286]
    (No symbol) [0x00007FF722C2F4BC]
    (No symbol) [0x00007FF722C82A27]
    (No symbol) [0x00007FF722C5728F]
    (No symbol) [0x00007FF722C7F6F3]
    (No symbol) [0x00007FF722C57023]
    (No symbol) [0x00007FF722C1FF5E]
    (No symbol) [0x00007FF722C211E3]
    (No symbol) [0x00007FF722D4D04F]
    (No symbol) [0x00007FF722D49614]
    (No symbol) [0x00007FF722D497B6]
    (No symbol) [0x00007FF722D38CE9]
    BaseThreadInitThunk [0x00007FF950C67374+20]
    RtlUserThreadStart [0x00007FF952B9CC91+33]`

    Need to position element screenshot


    As you can see. I want to click the select of taking action. But I can't click it.
    I ask for the Chatgpt. But I still can't solve it.

    I hope someone can help me to solve this problem.

    Continue reading...

Compartilhe esta Página