javascript 获取selenium web driver java中元素的类名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27506866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:34:06  来源:igfitidea点击:

Get class name of the element in selenium web driver java

javajavascriptseleniumselenium-webdriverwebdriver

提问by Prasad Magre

I am new to selenium web driver right now i am facing the issue while getting the class name of an element the html of the element look like

我现在是 selenium web 驱动程序的新手,我在获取元素的类名时遇到了这个问题,元素的 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="">

so i want to extract the class name which is class="detailbox detailbox-pattern unavailable-colour" by using selenium web driver. I got the element by the below code

所以我想通过使用 selenium web 驱动程序提取 class="detailbox detailbox-patternavailable-colour" 的类名。我通过下面的代码得到了元素

WebElement ele=driver.findElement(By.id("filter-colour-0237739001"));

now i want the class so can you please help me on this , i am okay with java script also

现在我想要这门课,所以你能帮我解决这个问题吗,我也可以使用 Java 脚本

回答by Saifur

Simply use getAttribute()

只需使用 getAttribute()

WebElement ele=driver.findElement(By.xpath("//*[@id='filter-colour-0237739001']/../div"));
ele.getAttribute("class")

EDITI guess you wanted the class of divso you should be using a selector pointing to div such as //*[@id='filter-colour-0237739001']/../divas xpath

编辑我想你想要的类div所以你应该使用指向 div 的选择器,例如//*[@id='filter-colour-0237739001']/../divxpath