我需要获得"数据密钥"标题" tr"中指定的元素值(标题"某些名称")。方法element.getAttribute(attributeName)返回null。我怎么能用Selenium WD + Java做到这一点?
<tr data-key="20"><td>Some name</td>...
@Test
public void test() throws Exception {
driver.navigate().to(some url);
WebElement elem = driver.findElement(By.xpath("//*[text() = 'Some name']"));
String val = elem.getAttribute("data-key");
System.out.print(val);
}
返回null
答案 0 :(得分:1)
xpath
标签是元素的父元素,带有文本&#34;有些名称&#34;。您可以使用..
父parent::
或WebElement element = driver.findElement(By.xpath("//*[text() = 'Some name']/.."));
// or By.xpath("//*[text() = 'Some name']/parent::tr");
element.getAttribute("data-key");
找到它
const next = {
...this.state,
[field]: value
}
this.setState(next)
}