Selenium 如何在robot框架中获取所有文本?
考虑以下源代码Selenium 如何在robot框架中获取所有文本?,selenium,selenium-webdriver,robotframework,Selenium,Selenium Webdriver,Robotframework,考虑以下源代码 <div id="groupContainer" class="XXXXXX"> <ul id="GroupContactListWrapper" class="list-wrapper"> <li class="contactNameItemContainer"> <div class="contactNameItem"> <span class="name">Nam
<div id="groupContainer" class="XXXXXX">
<ul id="GroupContactListWrapper" class="list-wrapper">
<li class="contactNameItemContainer">
<div class="contactNameItem">
<span class="name">Name1</span>
</div>
</li>
<li class="contactNameItemContainer">
<div class="contactNameItem">
<span class="name">Name2</span>
</div>
</li>
</ul>
</div>
请建议以下是获取Java中所有元素的逻辑。你可以根据需要采用它 必须使用findElement()而不是findElement()来获取所有元素
List<WebElement> items = driver.findElements(By.cssSelector("ul#GroupContactListWrapper div.contactNameItem"));
foreach(item in items){
System.out.println(item.getText();
}
List items=driver.findElements(By.cssSelector(“ul#GroupContactListWrapper div.contactNameItem”);
foreach(项目中的项目){
System.out.println(item.getText();
}
如果需要列表中的特定元素,可以使用items。get(1)get Text将返回与定位器匹配的第一个元素的内容。使用XPATH时,可以指定要获取的元素的索引,如下所示:
${name1} Get Text xpath=//div[@id='groupContainer']//li[@class='contactNameItemContainer'][0]//span
${name2} Get Text xpath=//div[@id='groupContainer']//li[@class='contactNameItemContainer'][1]//span
@{names} Create List ${name1} ${name2}
*** Settings ***
library Selenium2LibraryExt
*** Test Cases ***
Get All Texts Test
Open Browser http://www.example.com chrome
@{texts} Get All Texts css=.name
Log Many ${texts}
您可以扩展
Selenium2Library
并为此编写自己的关键字。将以下内容另存为Selenium2LibraryExt.py
from Selenium2Library import Selenium2Library
class Selenium2LibraryExt(Selenium2Library):
def get_all_texts(self, locator):
"""Returns the text value of elements identified by `locator`.
See `introduction` for details about locating elements.
"""
return self._get_all_texts(locator)
def _get_all_texts(self, locator):
elements = self._element_find(locator, False, True)
texts = []
for element in elements:
if element is not None:
texts.append(element.text)
return texts if texts else None
然后,您可以在测试中使用新的Get All text
关键字,如下所示:
${name1} Get Text xpath=//div[@id='groupContainer']//li[@class='contactNameItemContainer'][0]//span
${name2} Get Text xpath=//div[@id='groupContainer']//li[@class='contactNameItemContainer'][1]//span
@{names} Create List ${name1} ${name2}
*** Settings ***
library Selenium2LibraryExt
*** Test Cases ***
Get All Texts Test
Open Browser http://www.example.com chrome
@{texts} Get All Texts css=.name
Log Many ${texts}
您可以按如下方式迭代元素:
${xpath}= Set Variable //div[@id='groupContainer']//li[@class='contactNameItemContainer']//span
${count}= Get Matching Xpath Count ${xpath}
${names}= Create List
:FOR ${i} IN RANGE 1 ${count} + 1
\ ${name}= Get Text xpath=(${xpath})[${i}]
\ Append To List ${names} ${name}
这是可行的,但当有很多匹配项时,速度会相当慢。虽然排名最高的答案完全有效——而且最为xpath:),但让我添加一个我还没有看到的选项——使用
Get Webelements
关键字。例如:
@{locators}= Get Webelements xpath=//div[@id='groupContainer']//li[@class='contactNameItemContainer']//span
${result}= Create List
:FOR ${locator} in @{locators}
\ ${name}= Get Text ${locator}
\ Append To List ${result} ${name}
它将生成并返回所有匹配元素的列表,您只需对其进行迭代。它可能比xpath的[index]
引用快一点,因为dom只计算一次——但如果这不是完全正确的话,请不要让我负责:)@Velapanur
@{Texts}= Get WebElements ${AllTextboxes}
:FOR ${EachTextarea} in @{Texts}
\ Input Text ${EachTextarea} ${RandomTextdata}
我有另一个类似的要求,我必须在页面的文本区域中输入文本。
我在下面的文章中引用了托多尔的建议,并提出了这个想法,它起到了作用。非常感谢托多,维拉帕努尔
@{Texts}= Get WebElements ${AllTextboxes}
:FOR ${EachTextarea} in @{Texts}
\ Input Text ${EachTextarea} ${RandomTextdata}
对于上述情况,这是可行的,但我正在寻找一个更通用的,其中可能有2个以上的内部LI容器谢谢,但正在寻找Robot框架中的等效容器。谢谢,我已经知道了逻辑,只是想要一个Robot框架实现。显示用于1)检索列表的完整代码,2)检索文本。我可以问一下为什么使用“${name}”而不是“@{name}”作为列表名吗?我认为有一个拼写错误,xpath=(${xpath})[${I}]应该是xpath=(${xpath}),这就是为什么我认为测试用例有点复杂时RF很糟糕。它创建了一种新的语言,但使它比python更复杂,功能也更弱。为什么不让我们只使用python,而RF作为一个框架,只提供设置机制、拆卸、运行控制测试用例脚本、生成器报告等功能。够了,很好。现在它做的更多,但做的不好。关键词开发很糟糕。