获得元素的价值

时间:2016-07-07 15:21:47

标签: selenium selenium-webdriver

我需要在下面的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>

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分:0)

Java代码段

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

希望这会有所帮助。