Selenium webdriver get all the data attributes of an element

前端 未结 3 1784
孤城傲影
孤城傲影 2021-01-20 01:03

Using Selenium webdriver I have the following element

3条回答
  •  生来不讨喜
    2021-01-20 01:35

    This concept works very well. Try this!

    WebElement element = driver.findElement(By.tagName("button"));
    
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    Object elementAttributes = executor.executeScript("var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;",element);
    
    System.out.println(elementAttributes.toString());
    

提交回复
热议问题