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

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

我正在尝试从autosuggestion中选择新西兰。我试着用 $el.trigger("click"); 也一样,但它不起作用。

  1. it("select all tours location in the homepage", () => {
  2. cy.visit("https://www.bookmundi.com/"); //homepage
  3. cy.get('#atours-trigger').click({force:true})
  4. cy.get('#where-going').type('New');
  5. // cy.get('#where-going').click();
  6. cy.waitUntil(() => true);
  7. cy.get('#suggestion-div>ul>li>div>*').each(($el, index, $list) => {
  8. const prod = $el.text();
  9. const productToSelect = 'New Zealand';
  10. if(prod == productToSelect) {
  11. cy.wrap($el.find('a')).click({force:true});
  12. }
  13. })
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()

  1. 我想发生的是 `cy.waitUntil(() => true)` 等待的时间不多 `.each()` 在完全填充列表项之前抓取列表项
bvjveswy

bvjveswy2#

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

  1. cy.visit("https://www.bookmundi.com/");
  2. cy.get('#atours-trigger').click({ force: true })
  3. cy.get('#where-going').type('New Zealand');
  4. 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

相关问题