我尝试使用 cy.wrap() 因为 click() 不适用于自动吸入,但是仍然没有选择所需的选项

I tried using cy.wrap() since click() was not working for autosugession, however still the desired option is still not being selected

我正在尝试 select 来自自我暗示的国家新西兰。我也尝试使用 $el.trigger("click");,但它不起作用。

it("select all tours location in the homepage", () => {
        cy.visit("https://www.bookmundi.com/"); //homepage
        cy.get('#atours-trigger').click({force:true})
        cy.get('#where-going').type('New');
      //  cy.get('#where-going').click();
        cy.waitUntil(() => true);

        cy.get('#suggestion-div>ul>li>div>*').each(($el, index, $list) => {
            const prod = $el.text();
            const productToSelect = 'New Zealand';
            if(prod == productToSelect) {
                cy.wrap($el.find('a')).click({force:true});               
            }
        })

您可以等待 .should()

的“新西兰”建议
cy.visit("https://www.bookmundi.com/"); //homepage
cy.get('#atours-trigger').click({force:true})
cy.get('#where-going').type('New');

cy.contains('#suggestion-div div.item-list', 'New Zealand')
  .should('be.visible')                              // wait up to 4s
  .click()

我认为发生的事情是

  • cy.waitUntil(() => true) 没有太多等待

  • .each() 在完全填充之前抓取列表项

如果您使用 XPath 扩展,您可以用更少的代码实现相同的结果。

cy.visit("https://www.bookmundi.com/");
cy.get('#atours-trigger').click({ force: true })
cy.get('#where-going').type('New Zealand');
cy.xpath('(//a[contains(text(), "New Zealand")])[1]').click({ force: true })

请注意,为了使用 XPath,您必须使用以下命令安装它:npm install -D cypress-xpath 然后在您的项目中包含 cypress/support/index.js 这一行 require('cypress-xpath').

您可以在此处找到有关安装的更多详细信息:https://www.npmjs.com/package/cypress-xpath