【问题标题】:How to click on element from select list with puppeteer如何使用 puppeteer 从选择列表中单击元素
【发布时间】:2019-11-16 00:01:47
【问题描述】:

我正在尝试使用 puppeteer 在某个网站上创建自动化,但当我尝试单击选择列表中的元素时遇到了问题。

我在谷歌搜索后发现的所有内容都是选择一个项目,但只有当列表位于“选择”元素内时。 我的问题是我尝试自动化的 HTML 位于“div”内:

<div class="dropdown open dropdown--gray">
  <div class="dropdown__header">
     <div class="dropdown__field">other</div>
     <div class="dropdown__opener">
        <span class="icon icon-chevron-down"></span>
     </div>
  </div>
  <div class="dropdown__list">
    <div class="dropdown__list-item selected">other</div>
    <div class="dropdown__list-item">.org</div>
    <div class="dropdown__list-item">.co.uk</div>
    <div class="dropdown__list-item">.net</div>
    <div class="dropdown__list-item">.gov</div>
    <div class="dropdown__list-item">.de</div>
    <div class="dropdown__list-item">.fr</div>
    <div class="dropdown__list-item">.nl</div>
    <div class="dropdown__list-item">.com</div>
    <div class="dropdown__list-item">.be</div>
    <div class="dropdown__list-item">.jpg</div>
  </div>
</div>

我试过这个:

await page.click('div.dropdown__field');
const elementHandle4=await page.$$("div.dropdown__list-item");
await elementHandle4[8].click();

但实际上它并没有点击元素。 当我在此代码运行后手动打开列表时,我看到它向下滚动了列表的滚动条,但没有点击元素。

谢谢

【问题讨论】:

    标签: javascript node.js puppeteer


    【解决方案1】:

    试试这样的:

    await page.click('.dropdown__field');
    await page.click('.dropdown__list > div:nth-child(8)');
    

    更新:

    它看起来像视口中的问题,因为它可以正常工作并选择“.nl”选项而无需使用以下脚本进行任何滚动:

    (async () =>  {
        const browser = await puppeteer.launch({ 
            headless: false,
            defaultViewport: null,
        });
        const page = await browser.newPage();
        await page.goto('https://userinyerface.com/game.html');
        await page.click('.dropdown__field');
        await page.click('.dropdown__list > div:nth-child(8)');
    })();
    

    但如果我删除 defaultViewport: null 属性,它将不起作用

    【讨论】:

    • 我已经厌倦了这个。但它与帖子中描述的相同。
    • 通知消息@Yevhen
    • @GuyCohen 请您提供链接以尝试一下吗?在单击之前尝试滚动到元素。 await page.$$eval('.dropdown__list &gt; div', divs =&gt; divs[8].scrollIntoView());
    • 它向下滚动整个网站,但仍然不起作用。链接是:userinyerface.com/game.html 我尝试自动填写字段。问题在于“其他”列表。 @yevhen
    【解决方案2】:

    我猜你需要看到那个元素来触发点击事件。首先,必须滚动到元素的位置才能看到元素,然后触发点击事件。


    对我有用

    const ListItemIndex = 10, ListItem = document.querySelector(`.dropdown__list > div:nth-child(${ListItemIndex})`);
    document.querySelector(".dropdown__list").scroll({ behavior: 'smooth', top: ListItem.offsetTop });
    ListItem.click()
    

    【讨论】:

    • 如何在列表视图中向下滚动?当我尝试向下滚动时,它会滚动我的窗口。
    • 通知消息@Abdulsamet
    • 对于滚动,您需要运行并等待一段带有评估方法的 JavaScript 代码:document.querySelector(".dropdown__field").scroll({ behavior: 'smooth', top: 250 // List item top position });
    • 新的解决方案对我有用,你能再检查一下答案吗?
    • 因为它的 Puppeteer 应该是 async 和 await 等。所以我尝试的解决方案是:await page.evaluate(() =&gt; document.querySelector(.dropdown__list &gt; div:nth-child(8)``) ); await page.evaluate(() =&gt; document.querySelector(".dropdown__list").scroll({ behavior: 'smooth', top: document.querySelector(.dropdown__list > div:nth-child(8)`).offsetTop }) ) ;等待 page.click('.dropdown__list > div:nth-child(8)');'但它没有用。 @abdulsamet
    猜你喜欢
    • 2021-02-02
    • 1970-01-01
    • 2023-02-25
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多