首页 > 解决方案 > Cypress: How to get Href attribute value, redirect and match the URL with value

问题描述

I want to get the value (URL) of the first href for this class (.grid-item) then navigate to the respective href link and verify the value (URL) with the current page's URL.

How can I achieve that using Cypress?

Code:

  selectStackup = () => {
    cy.get(".grid-item")
      .eq(1)
      .invoke("attr", "href")
      .then(href => {
        cy.visit(href);
      });
  };

Error: - CypressError: cy.visit() must be called with a URL or an options object containing a URL as its 1st argument

标签: javascriptcypresschaining

解决方案


baseUrl只需添加from cypress.jsonOR的前缀yourUrl.com以及hrefand 就可以了。

cy.visit(`yourUrlOrBaseUrl.com${href}`);

推荐阅读