如何使用xpath从占位符获取值

use*_*510 7 xpath selenium-webdriver

所有元素都是动态的.我只能看到以下html中唯一的占位符: -

<input 
   id="ext-gen1617" 
   type="text" 
   size="20" 
   class="x-form-field x-form-text x-form-focus" 
   autocomplete="off" 
   aria-invalid="false" 
   placeholder="Gender" 
   data-errorqtip="" 
   role="textbox" 
   aria-describedby="combobox-1166-errorEl" 
   aria-required="true" 
   style="width: 78px;"
/>
Run Code Online (Sandbox Code Playgroud)

我需要显示值

placeholder="Gender". 
Run Code Online (Sandbox Code Playgroud)

我试过用

//input[@placeholder='Gender']
Run Code Online (Sandbox Code Playgroud)

但我的webdriver脚本无法识别它.

任何人都可以帮我解决它的可能解决方案吗?

小智 8

String s=driver.findElement(By.xpath("//input[@placeholder='Gender']")).getAttribute("placeholder"); 
System.out.println(s);
Run Code Online (Sandbox Code Playgroud)

要获取字段的属性,可以使用.getAttribute()方法.