You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

自动化测试脚本中动态元素定位失败且无法点击的问题求助

自动化测试脚本中动态元素定位失败且无法点击的问题求助

大家好,我遇到了一个自动化测试的棘手问题,想请各位帮忙看看:

问题描述

我在编写自动化测试脚本时,通过浏览器F12开发者工具可以正常找到目标元素,但脚本运行时完全无法点击这个元素。我已经尝试了多种方案来解决,但都没有效果。

已尝试的方法

我在代码中封装了两个显式等待元素的方法,但都没起作用:

等待元素出现并可见的方法

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.")

等待元素可点击并执行点击的方法

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.")

页面中的调用代码

问题出在self.select_action.click()这一行:

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

遇到的错误日志

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]

目标元素说明

我想要点击的是“执行操作”的下拉选择框,但始终无法成功。我已经咨询过ChatGPT,但还是没能解决问题,希望各位能帮我找到解决方案。

目标元素的HTML代码

下面是目标元素的HTML结构,我想要定位的是<div class="ant-select-selector">下的<span class="ant-select-selection-search">元素,但其中input的id是动态生成的,不知道该如何准确定位:

<div class="ant-space-item"><div class="ant-space css-1p4sm1f ant-space-horizontal ant-space-align-center ant-space-gap-row-small ant-space-gap-col-small"><div class="ant-space-item"><div class="ant-form-item css-1p4sm1f"><div class="ant-row ant-form-item-row css-1p4sm1f"><div class="ant-col ant-form-item-control css-1p4sm1f"><div class="ant-form-item-control-input"><div class="ant-form-item-control-input-content"><div class="ant-select ant-select-outlined ant-select-in-form-item ant-pro-filed-search-select pro-field pro-field-xs css-1p4sm1f ant-select-single ant-select-allow-clear ant-select-show-arrow" style="min-width: 100px;"><div class="ant-select-selector"><span class="ant-select-selection-search"><input type="search" id="ruleActionData_0_type" autocomplete="off" class="ant-select-selection-search-input" role="combobox" aria-expanded="false" aria-haspopup="listbox" aria-owns="ruleActionData_0_type_list" aria-autocomplete="list" aria-controls="ruleActionData_0_type_list" readonly="" unselectable="on" value="" style="opacity: 0;"></span><span class="ant-select-selection-item" title=""></span></div><span class="ant-select-arrow" unselectable="on" aria-hidden="true" style="user-select: none;"><span role="img" aria-label="down" class="anticon anticon-down ant-select-suffix"><svg viewBox="64 64 896 896" focusable="false" data-icon="down" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3.1-12.7-6.4-12.7z"></path></svg></span></span><span class="ant-select-clear" unselectable="on" aria-hidden="true" style="user-select: none;"><span role="img" aria-label="close-circle" class="anticon anticon-close-circle"><svg fill-rule="evenodd" viewBox="64 64 896 896" focusable="false" data-icon="close-circle" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M512 64c247.4 0 448 200.6 448 448S759.4 960 512 960 64 759.4 64 512 264.6 64 512 64zm127.98 274.82h-.04l-.08.06L512 466.75 384.14 338.88c-.04-.05-.06-.06-.08-.06a.12.12 0 00-.07 0c-.03 0-.05.01-.09.05l-45.02 45.02a.2.2 0 00-.05.09.12.12 0 000 .07v.02a.27.27 0 00.06.06L466.75 512 338.88 639.86c-.05.04-.06.06-.06.08a.12.12 0 000 .07c0 .03.01.05.05.09l45.02 45.02a.2.2 0 00.09.05.12.12 0 00.07 0c.02 0 .04-.01.08-.05L512 557.25l127.86 127.87c.04.04.06.05.08.05a.12.12 0 00.07 0c.03 0 .05-.01.09-.05l45.02-45.02a.2.2 0 00.05-.09.12.12 0 000-.07v-.02a.27.27 0 00-.05-.06L557.25 512l127.87-127.86c.04-.04.05-.06.05-.08a.12.12 0 000-.07c0-.03-.01-.05-.05-.09l-45.02-45.02a.2.2 0 00-.09-.05.12.12 0 00-.07 0z"></path></svg></span></span></div></div></div></div></div></div></div></div><div class="ant-space-item"></div></div></div>

备注:内容来源于stack exchange,提问作者user25710049

火山引擎 最新活动