我有此页面的以下功能文件:
@checkbox
Feature: Check the checkbox
Webdriver should be able to check the checkbox lists
Scenario: Check the checkbox
Given I am in checkbox task page
Then I can check the hobbies from the checkbox
Scenario Outline: Title of your scenario outline
Given I am in "http://suvian.in/selenium/1.6checkbox.html"
Then I can check the <id> from the checkbox
Examples:
| id |
| 1 |
| 2 |
| 3 |
| 4 |
问题是,我在我的场景大纲中涉及了像id
这样的细节。我想将其更改为
Examples:
| hobby |
| dancing |
| sporting |
| singing |
| PC Gaming |
但我不知道,如何解决该元素:
<input style="width:20px;height:20px;" type="checkbox" id="2">
在我的硒网络驱动程序中?
您可以使用xpath
preceding-sibling
//label[contains(.,'Singing')]/preceding-sibling::input
这将找到标签Singing
,并从那里找到以前的同级<input>
标签。
你可以尝试找出class='intro-message'下的所有标签
List<WebElement> labels = driver.findElements(By.xPath("//div[@class='intro-message']/label"));
然后尝试循环列表中的每个元素以读取其文本,如果这与您在黄瓜中传递的值匹配 示例,然后您可以获取该元素的属性值"for">
public WebElement getCheckboxLocator(string checkboxName){
int id = 0;
foreach(WebElement ele in labels){
if(ele.getAttribute("textContent").equalsIgnoreCase(checkboxName)){
id = Integer.parseInt(ele.getAttribute("for"));
}
}
return driver.findElement(By.xPath(string.format("//div[@class='intro-message']/input[id='%s']", id)));
}