请看下面的代码
`<div class="datepicker dropdown-menu" style="display: block; top: 301px; left: 308px;">
<div class="datepicker-days" style="display: block;">
<div class="datepicker-months" style="display: none;">
<div class="datepicker-years" style="display: none;">
</div>`
我正在尝试使用 classname(datepicker dropdown-menu") 获取 Div 中存在的 Div 的类名,但它可以与
wdriver.getAttribute("class").toString();
而不是
wdriver.getclass().toString();
。谁能告诉我有什么区别吗?
最佳答案
getclass() 和 getAttribute("class") 之间有很多区别
让我用例子向您展示。
首先
getclass() - 返回类型为 webelement
getAttribute("class") - 返回类型为字符串
例如
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1<mpl=default<mplcache=2&emr=1&osid=1#identifier");
Thread.sleep(4000L);
WebElement a = driver.findElement(By.xpath(".//*[@id='next']"));
String b = a.getAttribute("class");
System.out.println(b);
a.getClass();
System.out.println(a);
html代码
<input id="next" class="rc-button rc-button-submit" type="submit" value="Next" name="signIn"/>
b 将打印属性类的值,即“rc-button rc-button-submit”。
而 getClass() 会打印它的 xpath。即.//*[@id='next']
希望您现在可以了解为什么您没有从 getClass() 获取,因为返回类型不同。欢迎询问。快乐学习:-)
关于selenium-webdriver - Selenium 中 .getClass() 和 .getAttribute ("class")有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37412740/