2016-07-07 108 views
0

我需要在下面的HTML代碼中獲取data-id的值。獲取元素的值

我試圖代碼:如下圖所示

//p[contains(.,'Smart card')]/following-sibling::button[@data-id='633597015500043521']")); 

我的HTML代碼:

<form class="select-card-form" novalidate="" method="post"> 
     <input type="hidden" value="SmartCardSelect_3ca61d51-e601-40de-80fd-308bc47b52c6" name="FormName"> 
     <input type="hidden" value="d79cf158-93ad-4c77-b4bc-516ce8b28302" name="CardId"> 
     <div class="select-item "> 
       <p>Smart card 1</p> 
      <button class="submit-btn uniform-button button-smaller button-orange select-address" data-id="633597015500043521"> 
       <span>Select</span> 
      </button> 
     </div> 
    </form> 
+1

編程語言? – Andersson

+1

[在webdriver中如何獲取div中的屬性值]可能的副本(http://stackoverflow.com/questions/17592786/how-to-get-attribute-value-inside-a-div-in-webdriver) – JeffC

+0

請在發佈之前通過HTML美化工具運行您發佈的HTML,如http://www.cleancss.com/html-beautify/。它會使閱讀更容易。 – JeffC

回答

1

解決這個(JAVA):

WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.select-item > button")))); 
String value = element.getAttribute("data-id"); 
0

Java代碼片斷

WebElement element = driver.findElement(By.xpath("//form[@class='select-card-form']//div[@class='select-item ']//button")); 
element.getAttribute("data-id"); 

希望這會有所幫助。