无效的选择器:xpath表达式的结果。应该是一个要素

时间:2019-03-28 09:11:14

标签: javascript cucumber gherkin

我正在用javascript步骤编写硒中的自动化测试。在测试中,我想检查TEXT3是否等于TEXT3

网站的HTML代码:

<div _ngcontent-c35="" class="content">
       <p>
          TEXT1
           <u>
             <strong>TEXT2</strong>
           </u> 
           TEXT3
         </p>
</div>

JavaScript代码:

this.Then(/^also "([^"]*)" as loanPurposeInfoDialogLabel03 is displayed$/, function (fieldValue, next) {
    var fieldText = this.driver.findElement(By.xpath("//div[@class='content']/p/text()[2]"));
     fieldText.getText().then(getFieldValue => {
      assert.equal(getFieldValue, fieldValue, next, 'Expected value of loanPurposeInfoDialogLabel03 (Text) to be: ' + fieldValue);
    }, next);
  });

万一我在XPATH中使用/ text(),JavaScript代码将无法正常工作

//div[@class='content']/p/text()[2]

我收到错误消息:

InvalidSelectorError: invalid selector: The result of the xpath expression "//div[@class='content']/p/text()[2]" is: [object Text]. It should be an element.

2 个答案:

答案 0 :(得分:0)

由于要在xpath内提供text()函数,请尝试以下代码:

var fieldText = this.driver.findElement(By.xpath("//div[@class='content']/p/text[2]"));

答案 1 :(得分:0)

如果我使用xpath

"//div[@class='content']/p"

值是TEXT1 TEXT2 TEXT3,因此不必为TEXT3创建步骤

感谢SelçukAyhan:)