好的,我想从 div 和类中获取文本。 html 看起来像这样:
<div class="inventory_item_name">Backpack</div>
我的代码是这样的: const article = cy.get('.inventory_item_price').then((theElement) => {
theElement.text();
});
问题:当我做 cy.log(article)
我得到 Object{5}
最佳答案
就像评论中提到的亚历克斯一样,您不能从 cy
返回值命令。但是您可以在 .then
中这样做阻止就像:
it("should have text of 'Backpack'", () => {
// I have changed the selector since the class name in your HTML is ".inventory_item_name" not ".inventory_item_price"
cy.get(".inventory_item_name").then(($el) => {
const text = $el.text(); // Now you have the text "Backpack"
// Do the assertion here
expect(text).to.eq("Backpack");
});
});
您可以在此 documentation 中了解更多关于 Cypress 为什么不返回值的信息。
关于javascript - 如何从 div 类 Cypress 中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62932856/