如何在空手道中的变量中存储元素的文本值?

问题描述

我试图获取element的文本值并将其存储在变量中。

* configure driver = {type:'chrome'}
* driver 'https://www.webpagetest.org/'
* def orgName1 = text("//*[@id='header']/h1/a")
* def orgName2 = script("//*[@id='header']/h1/a",'_.textContent')
* print orgName1 
* print orgName1

这两种方法都不适合我。

* match text("//*[@id='header']/h1/a") == "WebPageTest"

工作正常

解决方法

请学习如何使用css选择器,这比XPath复杂度要好得多:

* def orgName1 = text('#header a')

如果您真的坚持使用XPath:

* def temp2 = text("//*[@id='header']//a")

通过这种方式对我也有效:

* def temp3 = text("//*[@id='header']/h1/a")

所以我不知道您身边缺少什么。我一直要求您遵循此过程,但我想那永远不会发生:https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue