【问题标题】:How to click on a drop-down item using .elementIdClick() in nightwatch?如何在守夜人中使用 .elementIdClick() 单击下拉项?
【发布时间】:2020-05-10 07:01:45
【问题描述】:

我想使用 nightwatch 从下拉列表中选择一个项目。我能够拿到这个项目,直到我的 if 条件。它还打印值。但是当我使用 elementIdClick 单击该项目时,它会返回 {"status":-1,"state":"","value":{"message":"element click intercepted:

     await browser.elements('css selector','#profile-container .col-md-6 .pd-dropdown-wrapper ul li', async function (res) {
     await res.value.forEach(async function (jsonWebElement) {
         let jsonWebElementId = jsonWebElement.ELEMENT;
         await browser.elementIdText(jsonWebElementId, async function (jsonElement) {
             let text = jsonElement.value;
             console.log('text++++',text)
             if(text == 'Last 30 Days'){
              console.log('text^^^^^',text)
              await browser.elementIdClick(jsonWebElement.ELEMENT);
             }
         });
        });
     });

【问题讨论】:

  • 我试过这个browser.execute('document.querySelector("#transaction-container .col-md-6 .pd-dropdown-wrapper ul li a").options[2].selected=true')。它没有从下拉列表中选择选项。它也没有抛出任何错误消息。
  • 这是一个工作示例:codepen.io/mattgreen/pen/gObExaa 您应该能够在 chrome 中访问该页面,并打开 devtools 进行试验(控制台上的 JavaScript)。确保您的选择器是正确的。
  • 有什么想法吗?我相信我快到了,但我无法从 if 条件下的下拉列表中选择值。但在“console.log”中我可以看到过滤后的值。

标签: javascript selenium-webdriver nodes nightwatch.js


【解决方案1】:

这适用于使用 Nightwatch 1.3.2 的标准选择列表

 browser
    .url('http://icreatehtml.co.uk/select.html')
    .waitForElementVisible('#pet-select')
     // Open the select list
    .click('#pet-select') 
    // Set the value using the visible text
    .click('xpath', `//select[@id='pet-select']//option[contains(text(),'Hamster')]`)
    // Close select list
    .click('body')
    .pause(3*1000)
    // Open the select list
    .click('#pet-select')
    // Set the value using the option value
    .click('#pet-select option[value="cat"]')
    // Close select list
    .click('body')
    .pause(3*1000)
    // Open the select list
    .click('#pet-select')
    // Set the value using a pseudo selector
    .click('#pet-select option:last-child')
    // Close select list
    .click('body')
    .end();

【讨论】:

  • 如果下拉菜单上有 option 标签,我在选择值时没有问题。当下拉列表有ul li 标签时,看起来很难选择一个值。正常的策略不起作用。例如<ul data-v-24d95aa5="" class="pd-dropdown-menu"> <li data-v-24d95aa5="" class="py-2 px-3 d-flex justify"> <a data-v-24d95aa5="" title="Click here" href="#" target="_self" class="options">All Countries</a></li>
  • 我的回答对 JS 库生成的选择和列表都有效。第一个动作需要打开列表,第二个动作是点击需要的值。而是
  • 如果您没有属性来标识您想要选择的内容,那么唯一的选择就是使用 xpath。 browser .waitForElementVisible('[打开列表的元素选择器]') // 打开选择列表 .click('[打开列表的元素选择器]') // 使用可见文本设置值 .click(' xpath', //ul[@id='pet-select']//li//a[contains(text(),'All Countries')])
  • 我正在寻找的答案是创建一个自定义命令来从列表框中选择一个值。这样我就可以尽可能使用该命令。目前我可以通过单击主列表框并通过传递 css 再次单击该值来实现。我正在寻找的解决方案是通过传递我要选择的文本值来定制命令。
猜你喜欢
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多