对于这个HTML:
<ul class="listnav" id="yui_3_16_0_1_1496654440419_3201" role="presentation">
<li class="compose hasmultimsg primary-property-btn selected" id="Compose" role="presentation" data-fid="Compose">
<button tabindex="0" title="Compose" class="btn btn-compose" id="yui_3_16_0_1_1496654440419_3200" data-action="compose">
<i></i>
<span class="icon-text" id="yui_3_16_0_1_1496654440419_3199">Compose</span>
</button>
</li>
</ul>
我想检索&#39;撰写&#39;的ID
属性。按钮。
这是我的代码:
By btnBy = By.xpath("//div[@id = 'shellnavigation']/div/ul/li[@id = 'Compose']/button[@class='btn btn-compose']");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(btnBy));
WebElement btnCompose = driver.findElement(By.xpath("//div[@id = 'shellnavigation']/div/ul/li[@id = 'Compose']/button[@class='btn btn-compose']"));
System.out.println("Text is : " + btnCompose.getText()
+ " Class is: " + btnCompose.getAttribute("class")
+ " Id is: " + btnCompose.getAttribute("id")
+ " Title is : " + btnCompose.getAttribute("title")
+ "innerHTML " + btnCompose.getAttribute("innerHTML"));
我的设置方法是:
@Before
public void setUp() throws Exception {
try {
if (driver != null)
driver.quit();
else {
driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
java.util.logging.Logger.getLogger("com.gargoylesoftware")
.setLevel(Level.OFF);
driver.manage().window().maximize();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
我使用以下配置:
Windows 10,
Selenium Sever standalone 3.4.0,
HTML驱动程序。
我在控制台中看到以下输出:
文字是:Compose Class是:btn btn-compose Id is:null标题是: 撰写innerHTML
<i class=""></i><span class="icon-text">Compose</span>
因此id属性的值为null,其他属性为OK。
我不知道为什么它显示为null
。