2017-07-16 52 views
0

我有this page以下特點文件:Selenium:地址複選框的標籤?

@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 | 

的問題是,我已經參與thecnical細節,如id在我的方案大綱。我想改變它作爲

Examples: 
      | hobby  | 
      | dancing | 
      | sporting | 
      | singing | 
      | PC Gaming | 

但我不知道,如何解決元素:

<input style="width:20px;height:20px;" type="checkbox" id="2"> 

在我的硒的webdriver?

回答

0

你可以嘗試找出所有下級=「前奏消息」

List<WebElement> labels = driver.findElements(By.xPath("//div[@class='intro-message']/label")); 

標籤然後嘗試循環的每個元素在列表中閱讀其文字內容,如果該值匹配您傳遞在黃瓜的例子,那麼你可以「爲」獲取的屬性值,該元素

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))); 
} 
1

您可以使用xpathpreceding-sibling

//label[contains(.,'Singing')]/preceding-sibling::input 

這將找到標籤Singing,並從那裏找到之前的兄弟<input>標籤。

+1

這是一個更好的解決方案。 – Sameer