invalid selector:xpath表达式的结果。它应该是一个元素

问题描述 投票:1回答:2

我正在使用javascript步骤在selenium中编写自动化测试。在测试中,我想检查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.
javascript cucumber gherkin
2个回答
0
投票

因为您在xpath中提供text()函数,请尝试以下代码:

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

0
投票

如果我使用xpath

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

值为TEXT1 TEXT2 TEXT3,因此无需为TEXT3创建步骤

感谢SelçukAyhan:)

© www.soinside.com 2019 - 2024. All rights reserved.