【问题标题】:Get class name of the element in selenium web driver java在 selenium Web 驱动程序 java 中获取元素的类名
【发布时间】:2015-02-14 21:30:24
【问题描述】:

我现在是 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="">

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

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

现在我想要上课,请你帮我解决这个问题,我也可以使用 java 脚本

【问题讨论】:

    标签: java javascript selenium selenium-webdriver webdriver


    【解决方案1】:

    只需使用getAttribute()

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      • 1970-01-01
      • 2016-11-02
      • 2015-09-19
      • 2020-12-29
      • 1970-01-01
      相关资源
      最近更新 更多