Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Selenium WebDriver findElement(By.xpath())获取此元素的值_Java_Selenium - Fatal编程技术网

Java Selenium WebDriver findElement(By.xpath())获取此元素的值

Java Selenium WebDriver findElement(By.xpath())获取此元素的值,java,selenium,Java,Selenium,部分内容如下所示: <input type="hidden" id="recaptcha-token" value="Need This Value"> driver.findElement(By.xpath("[@id=\"recaptcha-token\"]")); 如何从运行此代码中获得“需要此值”?对于您的特定情况,您可以先获得如下元素: driver.findElement(By.xpath("//*[@id='recaptcha-token']")

部分内容如下所示:

<input type="hidden" id="recaptcha-token" value="Need This Value">
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()
来简化该调用:

可能重复的可能重复请参见答案的
By.xpath
部分,
driver.findElement(By.id("recaptcha-token"));