![](/img/trans.png)
[英]Selenium WebDriver - Selecting webelements with findElements(By.xpath
[英]Selenium WebDriver findElement(By.xpath()) get value of this element
它的一部分看起來像這樣:
<input type="hidden" id="recaptcha-token" value="Need This Value">
我正在像這樣運行 Selenium:
driver.findElement(By.xpath("[@id=\"recaptcha-token\"]"));
如何通過運行此代碼獲得“需要此值”?
對於您的特定情況,您可以首先像這樣獲取元素:
driver.findElement(By.xpath("//*[@id='recaptcha-token']")); // Any element with that id
driver.findElement(By.xpath("//input[@id='recaptcha-token']")); // More specific to your tag
然后你使用getAttribute(String attrName)
獲得你想要的屬性。
一個單班輪將是:
driver.findElement(By.xpath("//input[@id='recaptcha-token']")).getAttribute("value");
如果你只是想尋找與該ID的元素,你可以簡化使用該呼叫By.id()
代替By.xpath()
driver.findElement(By.id("recaptcha-token"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.