# 问题背景
使用Browser库写的UI自动化用例,在本地测试是可以正常跑过的,提交代码后服务器跑不通过,报错如下,并且我打开有头浏览器可以看到元素是正常加载完成了的
TimeoutError: locator.waitFor: Timeout 30000ms exceeded.
Call log:
- waiting for locator('iframe[name="pdiv_page-mainIframe"]').contentFrame().locator('iframe').contentFrame().locator('role=columnheader[name="开始时间(秒)"]') to be visible
元素本身如下
<th _ngcontent-ng-c691599427="" title="开始时间(秒)" class="ng-star-inserted"><div class="plx-table-x-th-content"><span _ngcontent-ng-c691599427="">开始时间(秒)</span><!----><plx-table-x-resize _ngcontent-ng-c691599427="" class="ng-star-inserted"><span class="table-x-resize-line"></span></plx-table-x-resize><!----></div><!----><!----><!----><!----></th>
原代码如下:
对应Python: expect(page.locator("iframe[name=\"pdiv_page-mainIframe\"]").content_frame.locator("iframe").content_frame.get_by_role("columnheader", name="开始时间(秒)")).to_be_visible()
等待元素出现 ${NESTED_IFRAME} >>> role=columnheader[name="开始时间(秒)"]
# 问题原因 (deepseek给的)
核心矛盾 :你使用的是 role=columnheader[name="开始时间(秒)"] 定位器,但浏览器在无头模式下为该 <th> 计算出的可访问性名称可能不是 " 开始时间(秒)" ,因为文本节点内容是 " 开始时间(秒) "(前后有空格),而可访问性名称通常会被规范化(trim)。此外,role=columnheader 依赖于可访问性树的构建,无头模式下的计算时序有延迟,导致超时。
# 解决方法
修改元素定位方式,建议表格有表头的不要使用role=columnheader这种定位方式
Wait For Elements State ${NESTED_IFRAME} >>> th:has-text("开始时间(秒)") visible timeout=60s