【问题标题】:xpath span not detected未检测到 xpath 跨度
【发布时间】:2021-12-22 22:44:18
【问题描述】:

更新了外部 html 和最新的错误。似乎检测到了该元素,但未能点击它。

我的脚本在下面的元素处失败,我尝试了很多方法来重建 xpath,但机器人不断失败,DOM 无效。

原始元素:

<div class="ant-select SearchPrompter_advInput__3P9Jf ant-select-multiple ant-select-allow-clear ant-select-show-search">
<div class="ant-select-selector">
    <div class="ant-select-selection-overflow">
        <div class="ant-select-selection-overflow-item ant-select-selection-overflow-item-suffix" style="opacity: 1;">
            <div class="ant-select-selection-search" style="width: 3px;">
                <input autocomplete="off" type="search" class="ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_2_list" aria-autocomplete="list" aria-controls="rc_select_2_list" aria-activedescendant="rc_select_2_list_0" value="" id="rc_select_2" style="opacity: 0;" aria-expanded="false" readonly="" unselectable="on">
                    <span class="ant-select-selection-search-mirror" aria-hidden="true">&nbsp;</span>
                </div>
            </div>
        </div>
        <span class="ant-select-selection-placeholder">Select Source(s)</span>
    </div>
</div>

脚本:

*** Settings ***
Library    Browser
Library    OperatingSystem
Resource   ../Resources/BrowserParameters.robot
Resource   ../Resources/BrowserResources.robot
Resource   ../Resources/BrowserCustomKeywords.robot

#Select Browser: chromium or firefox
Test Setup      Test Setup    Browser=chromium
Test Teardown   Test Teardown

*** Test Cases ***
001
   Click     //span[contains(@class,'ant-select-selection-placeholder') and contains(text(),'Select Source(s)')]

错误

    FAIL
Message:    TimeoutError: locator.click: Timeout 10000ms exceeded.
=========================== logs ===========================
waiting for selector "//span[contains(@class,'ant-select-selection-placeholder') and contains(text(),'Select Source(s)')] >> nth=0"
  selector resolved to hidden <span class="ant-select-selection-placeholder">Select Source(s)</span>
attempting click action
  waiting for element to be visible, enabled and stable
    element is not stable - waiting...
  element is visible, enabled and stable
  scrolling into view if needed
  done scrolling
  checking that element receives pointer events at (1080.4,304.7)
  <div class="ant-select-selection-overflow">…</div> intercepts pointer events
retrying click action, attempt #1
  waiting for element to be visible, enabled and stable
  element is visible, enabled and stable
  scrolling into view if needed
  done scrolling
  checking that element receives pointer events at (1080.4,304.7)
    [ Message content over the limit has been removed. ]
  element is visible, enabled and stable
  scrolling into view if needed
  done scrolling
  checking that element receives pointer events at (1080.4,304.7)
  <div class="ant-select-selection-overflow">…</div> intercepts pointer events

如果我使用它,那么我可以看到正在访问的字段并显示列表。但是在表单中有 2 个字段使用相同的 xpath。所以机器人进入了第一个字段,但我希望它进入下一个字段。

Click     //div[@class="ant-select SearchPrompter_advInput__3P9Jf ant-select-multiple ant-select-allow-clear ant-select-show-search"]

【问题讨论】:

    标签: css selenium xpath robotframework


    【解决方案1】:

    请更正您的 Xpath 来自

    //span[@class=""ant-select-selection-placeholder">Select Source(s)"]
    

    //span[@class="ant-select-selection-placeholder"]
    

    或者

    //span[@class="ant-select-selection-placeholder" and (text()="Select Source(s)")]
    

    【讨论】:

    • 我需要这个才能让它独一无二。 “选择来源”
    • "Select Source(s)" 是文本,没有类名。我会更新答案
    • 用新错误更新了主帖。我检查了 xpath 是否突出显示,但光标似乎仍然没有指向它。以手动方式,当我单击该字段时,将列出一个值列表。
    【解决方案2】:

    此错误消息...

    Error: locator.click: DOMException: Failed to execute 'evaluate' on 'Document': The string './/span[@class=""ant-select-selection-placeholder">Select Source(s)"]' is not a valid XPath expression.
    

    ...暗示您使用的 XPath 不是有效的 表达式。

    你必须做如下两个小调整:

    • 类属性的值必须在单双引号内,即"value"
    • Select Source(s) 是里面的innerText,所以你必须把它写成text

    解决方案

    您可以使用以下任一Locator Strategies

    • xpath 1

      //span[@class='ant-select-selection-placeholder' and starts-with(., 'Select Source')]
      
    • xpath 2

      //span[@class='ant-select-selection-placeholder' and contains(., 'Select Source')]
      

    【讨论】:

    • 用新错误更新了主帖。我检查了 xpath 是否突出显示,但光标似乎仍然没有指向它。以手动方式,当我单击该字段时,将列出一个值列表。
    【解决方案3】:

    请使用下面的xpath

    //span[contains(@class,'ant-select-selection-placeholder') and contains(text(),'Select Source(s)')]
    

    //span[contains(@class,'ant-select-selection-placeholder') and starts-with(text(),'Select Source')]
    

    如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。

    检查步骤:

    Press F12 in Chrome -> 转到element 部分 -> 执行CTRL + F -> 然后粘贴xpath 看看,如果您想要的element 用@ 得到突出显示 987654330@匹配节点。

    如果我们有 1/1 匹配节点,请确保:

    1. 此 div 不在 iframe 下。
    2. 此 div 不在 shadow-root 下。
    3. 您不应该在 selenium 启动的新选项卡/窗口上。

    【讨论】:

    • 用新错误更新了主帖。我检查了 xpath 是否突出显示,但光标似乎仍然没有指向它。以手动方式,当我单击该字段时,将列出一个值列表。
    猜你喜欢
    • 2013-04-20
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 2018-11-15
    相关资源
    最近更新 更多