简体   繁体   English

如何使Puppeteer从antd Select(非本地选择元素)中选择项目?

[英]How to get Puppeteer to select item from antd Select (non-native select element)?

I'm trying to get puppeteer to work with antd Select but can't figure out how. 我试图让puppeteer与antd Select配合使用,但不知道如何操作。 Puppeteer keeps timing out because it can't find the selector. 由于找不到选择器,Puppeteer一直处于超时状态。

<Select
    id="select-whale-type-dropdown"
    showSearch
    placeholder="Choose one..."
    notFoundContent="Whale Type not found"
>
<Select.Option value="Blue Whale" key="Blue Whale">
    Blue Whale
</Select.Option>
<Select.Option value="Humpback Whale" key="Humpback Whale">
    Humpback Whale
</Select.Option>
<Select.Option value="Pilot Whale" key="Pilot Whale">
    Pilot Whale
</Select.Option>

Error message: 错误信息:

1) Frontend crawl test
   Whale crawl
     Selects Humpback Whale as Whale Type:
 Error: waiting for selector "#select-whale-type-dropdown" failed: timeout 30000ms exceeded
  at Timeout.WaitTask._timeoutTimer.setTimeout (node_modules/puppeteer/lib/FrameManager.js:845:60)

It worked fine when we were still using react-bootstrap's dropdown, it seems like antd doesn't use the native select element. 当我们仍在使用react-bootstrap的下拉菜单时,它运行良好,似乎antd没有使用本机的select元素。 Any idea how to get antd Select to work with puppeteer? 知道如何让antd Select与puppeteer一起使用吗?

Thank you :) 谢谢 :)

Jess 杰斯

we can use aria-controls attribute now . 我们现在可以使用aria-controls属性。

<div class="ant-select-selection
        ant-select-selection--single" role="combobox" aria-autocomplete="list" aria-haspopup="true" aria-controls="dc58f58e-d287-4a0b-a9aa-d5ce3435102a" aria-expanded="false" data-__meta="[object Object]" data-__field="[object Object]" tabindex="0">

then 然后

document.querySelectorAll('#dc58f58e-d287-4a0b-a9aa-d5ce3435102a')

You are correct, the native <select> element is not used. 没错,没有使用本机的<select>元素。 AntD <Select> has a lot more features than it can support. AntD <Select>具有的功能超出了其支持范围。

So you need to take a look at what AntD actually renders, and direct Puppeteer to look for that code. 因此,您需要查看AntD实际呈现的内容,并指示Puppeteer查找该代码。 The input field part looks like this: 输入字段部分如下所示:

<div class="ant-select ant-select-enabled">
    <div class="ant-select-selection
            ant-select-selection--single" role="combobox" aria-autocomplete="list" aria-haspopup="true"
         aria-expanded="false" tabindex="0">
        <div class="ant-select-selection__rendered">
            <div unselectable="unselectable" class="ant-select-selection__placeholder"
                 style="display: block; user-select: none;">Choose one...
            </div>
            <div class="ant-select-search ant-select-search--inline" style="display: none;">
                <div class="ant-select-search__field__wrap">
                    <input id="select-whale-type-dropdown" autocomplete="off"
                           class="ant-select-search__field" value="">
                    <span class="ant-select-search__field__mirror">&nbsp;</span>
                </div>
            </div>
        </div>
        <span class="ant-select-arrow" unselectable="unselectable" style="user-select: none;"><b></b></span></div>
</div>

Then the dropdown is rendered as an absolutely positioned div on top of the page, looking like this: 然后,将下拉列表呈现为页面顶部绝对定位的div,如下所示:

<div style="position: absolute; top: 0px; left: 0px; width: 100%;">
    <div>
        <div class="ant-select-dropdown ant-select-dropdown--single ant-select-dropdown-placement-bottomLeft  ant-select-dropdown-hidden"
             style="width: 400px; left: 198.5px; top: 1196.08px;">
            <div style="overflow: auto;">
                <ul class="ant-select-dropdown-menu  ant-select-dropdown-menu-root ant-select-dropdown-menu-vertical"
                    role="menu" aria-activedescendant="" tabindex="0">
                    <li unselectable="unselectable" class="ant-select-dropdown-menu-item" role="menuitem"
                        aria-selected="false" style="user-select: none;">Blue Whale
                    </li>
                    <li unselectable="unselectable" class="ant-select-dropdown-menu-item" role="menuitem"
                        aria-selected="false" style="user-select: none;">Humpback Whale
                    </li>
                    <li unselectable="unselectable" class="ant-select-dropdown-menu-item" role="menuitem"
                        aria-selected="false" style="user-select: none;">Pilot Whale
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>

So Puppeteer should look for the input#select-whale-type-dropdown field, click it and then look for ul>li menu item tags. 因此,Puppeteer应该查找input#select-whale-type-dropdown字段,单击它,然后查找ul> li菜单项标签。 If you have several <Select> on the page, use the dropdownClassName property to assign different class names to the absolutely positioned divs, in order to make them individually addressable. 如果页面上有多个<Select> ,请使用dropdownClassName属性为绝对定位的div分配不同的类名称,以使其分别可寻址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM