我现在是 selenium 网络驱动程序的新手,我在获取元素的类名时遇到了问题 元素的 html 看起来像
<input id="filter-colour-0237739001"><div class="detailbox detailbox-pattern unavailable-colour"><span></span><img src="//lp2.hm.com/hmprod?set=source[/fabric/2014/9B57A69A-FD8D-4D79-9E92-8F5448566C51.jpg],type[FABRICSWATCH]&hmver=0&call=url[file:/product/main]" alt="" title="">
所以我想通过使用 selenium 网络驱动程序提取类名 class="detailbox detailbox-pattern unavailable-colour"。 我通过下面的代码得到了元素
WebElement ele=driver.findElement(By.id("filter-colour-0237739001"));
现在我想要这个类(class),所以你能帮我解决这个问题吗,我也可以使用 java 脚本
最佳答案
只需使用getAttribute()
WebElement ele=driver.findElement(By.xpath("//*[@id='filter-colour-0237739001']/../div"));
ele.getAttribute("class")
编辑 我猜你想要 div
类,所以你应该使用一个指向 div 的选择器,例如 //*[@id='filter -colour-0237739001']/../div
作为 xpath
关于java - 获取selenium web driver java中元素的类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27506866/