从 selenium/protractor javascript 中的 span 元素中提取内部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29377734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:26:47  来源:igfitidea点击:

Extract inner from a span element in selenium/protractor javascript

javascriptseleniumasynchronousprotractor

提问by William Roberts

I have a span element on my page. I was wondering how I could extract the text in between the span opening tag and span closing tag. I'm using selenium with protractor in javascript.

我的页面上有一个 span 元素。我想知道如何提取跨度开始标记和跨度结束标记之间的文本。我在 javascript 中使用 selenium 和量角器。

<span....>
Inner text
</span>

回答by alecxe

It is actually called a text of an element. Use getText():

它实际上被称为元素的文本。使用getText()

var elm = element(by.id("myid"));
expect(elm.getText()).toEqual("My Text");

Note that getText()as many other methods in protractor returns a promise. expect()"knows" how to resolve the promise - it would wait until the real text value would be retrieved and only then perform an assertion.

请注意,getText()量角器中的许多其他方法都会返回一个承诺。expect()“知道”如何解决承诺——它会等到真正的文本值被检索到,然后才执行断言。

If you want to see an actual text value on the console, resolve the promise with then():

如果您想在控制台上看到实际的文本值,请使用以下命令解决承诺then()

elm.getText().then(function (text) {
    console.log(text);
});

回答by Tom Nijs

To find elements on a web page, Protractor uses locators. Read up on them here: https://github.com/angular/protractor/blob/master/docs/locators.mdSince you haven't posted any code, this may differ from the solution you need. The locator I'll use is by using its ID.

为了在网页上查找元素,Protractor 使用定位器。在此处阅读它们:https: //github.com/angular/protractor/blob/master/docs/locators.md由于您尚未发布任何代码,因此这可能与您需要的解决方案不同。我将使用的定位器是使用它的 ID。

var foo = element(by.id('yourspanID')).getText();