英文:
ReferenceError : $el is not defined in cypress []
问题
///\<reference types = "cypress"/\>
///\<reference types = "cypress-xpath"/\>
describe("与下拉菜单交互", function () {
  it("自动建议动态下拉菜单:我们需要编写函数", function () {
    cy.visit("https://www.google.com/");
    cy.wait(2000);
    cy.get(".gLFyf").type("自动化测试");
    cy.wait(5000);
    cy.get(".wM6W7d>span").should("have.length", "12");
    cy.get(".wM6W7d>span").each(($el, index, $list) => {
      if ($el.text() == "自动化测试教程") {
        cy.wrap($el).click();
      }
    });
    cy.get(".gLFyf").should("have.value", "自动化测试");
  });
});
在执行时,我遇到了"ReferenceError",无法在Cypress中执行代码,并且出现了"$el未定义"的错误。
英文:
///\<reference types = "cypress"/\>
///\<reference types = "cypress-xpath"/\>
describe("Interacting with dropdowns", function () {
  it("Auto Suggest Dynamic Dropdown: We need to write function", function () {
    cy.visit("https://www.google.com/");
    cy.wait(2000);
    cy.get(".gLFyf").type("Automation Testing");
    cy.wait(5000);
    cy.get(".wM6W7d>span").should("have.length", "12");
    cy.get(".wM6W7d>span").each(($el, index, $list), function () {
      if ($el.text() == "automation testing tutorial") {
        cy.wrap($el).click();
      }
    });
    cy.get(".gLFyf").should("have.value", "Automation Testing");
  });
});
While execution I am getting referenceerror.Unable to execute the code in cypress and also getting the "ReferenceError" i.e., $el is not defined .
答案1
得分: 1
你可以尝试运行这段代码,并根据你的需要进行更新。
以下是结果的快照:
在这里,借助这个方法的帮助,我们检索了结果并根据匹配条件执行了点击事件。
我相信每个人都不能正确地找到可迭代对象。
有关每个的更多详细信息,请查看文档链接:
https://docs.cypress.io/api/commands/each
希望这有所帮助!
祝愉快编码 ![]()
英文:
You can try out this code and update it according to your need.
describe("Interacting with dropdowns", function () {
  it("Auto Suggest Dynamic Dropdown: We need to write function", function () {
    cy.visit("https://www.google.com/");
    cy.wait(2000);
    cy.get(".gLFyf").type("Automation Testing");
    cy.wait(5000);
    cy.get(".wM6W7d>span").should("have.length", "12");
    cy.get(".wM6W7d>span").then((result) => {
      for (const item of result) {
        if (item.innerText == "automation testing tutorial") {
          cy.get(`.${item.offsetParent.className}`).click();
        }
      }
      cy.wait(2000);
      cy.contains(`Automation Testing`);
    });
  });
});
Here is the snap of the result:
Here with the help of the method, we retrieved the result and executed the click event based on the matching condition.
I believe each is not able to properly find the iterable.
For more detail regarding each, please go through the documentation link :
https://docs.cypress.io/api/commands/each
Hope this helps!
Happy coding ![]()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


![ReferenceError: $el在Cypress中未定义[]](https://i.stack.imgur.com/7jHod.png)
评论