【问题标题】:Protractor code to extract multiple paragraph text量角器代码提取多个段落文本
【发布时间】:2017-10-30 00:15:11
【问题描述】:

如何一次获取所有段落 (p) 的值.. 例如下面是我的检查视图的样子

“测试样品一。” “测试样品二。”

下面是我提取 id 'run' 值的代码示例

browser.findElement(by.css('[id=run]')).getText()

这只是提取第一个值,或者我可以修改并获取 id 的第二个值..我需要一次获取两个值..在同一行代码中..您能否建议

【问题讨论】:

  • browser.findElements(by.css('[id=run]')).getText() 应该可以工作吗?

标签: javascript selenium protractor


【解决方案1】:

虽然缺少一个 html 示例和更多关于您想要提取的内容的描述,但我会尝试一下。

一般来说,您可以/应该使用element.all(by.css()),也就是$$(),而不是browser.findElements,除非您确切知道为什么使用findElementsRead some details about the difference here.

然后正如评论中已经提到的,有一个findElements() (see API-Doc for findElements() here),返回一个包含所有值的数组,符合条件。只是你不能立即在上面使用getText(),因为你得到了一个元素数组,而getText() 需要一个元素(see API-Doc for getText() here)。因此,您需要通过一些循环来传递它。

在没有足够的背景知识的情况下,这里有一小部分可供选择的想法。

var allP = new Array();
var allPString = null; //if one long string is desired
//here I'm using now element.all() instead of browser.findElements
var allPEl = $$('p#run'); //equal to element.all(by.css('p[id=run]')); //returns array of all found elements
var allPElBrowser = browser.findElements(by.id('run')); //returns array of all found elements
var i = allPEl.length();
var j = 0;

allPEl.each().getText().then(function(text){  //getAttribute('value') instead of getText(), if getText doesn't work.
    allP.push(text); //add it to Array
    allPString += text+' '; //add to String with a space at the end
    j++; //counter
    if(i === j-1){continueTest()}; //call continuation at the end of last loop, due to asynchronous nature of 'then()'.
});

continueTest = function(){
    allP.toString() //in case of comma separated list from Array is desired
    // here comes the rest of your test case logic
};

请注意,我假设您需要已解决的承诺,因此您的 <p> 的内容将继续。

如果您可以继续使用 <p>-objects 数组,稍后您将在 expect() 中解析它,那么您只需要 $$('p#id');

如果解决方案对您不起作用,请告诉我,哪些部分仍然缺失或哪里出现问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 2022-12-04
    相关资源
    最近更新 更多