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

gc0ot86w  于 2021-09-13  发布在  Java
关注(0)|答案(2)|浏览(288)

我正在尝试从autosuggestion中选择新西兰。我试着用 $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});               
            }
        })
ifsvaxew

ifsvaxew1#

你可以等待“新西兰”的建议 .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()` 在完全填充列表项之前抓取列表项
bvjveswy

bvjveswy2#

如果使用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

相关问题