Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取Selenium中选定元素的所有CSS属性的值_Javascript_Java_Css_Selenium_Xpath - Fatal编程技术网

Javascript 获取Selenium中选定元素的所有CSS属性的值

Javascript 获取Selenium中选定元素的所有CSS属性的值,javascript,java,css,selenium,xpath,Javascript,Java,Css,Selenium,Xpath,假设我通过XPath找到了一个元素,使用: WebElement we=driver.findElement(By.xpath(“somexpath”) 我知道我可以通过we.getCssValue(“some property”)获得特定CSS属性的值,但是我可以获得所有属性的值而不必明确提及它们的名称吗? 这在本机Selenium API中是不可能的 但使用Javascript,您可以: 您可以使用Seleniums的JavascriptExecutor.executeScript功能来使用

假设我通过XPath找到了一个元素,使用:

WebElement we=driver.findElement(By.xpath(“somexpath”)

我知道我可以通过
we.getCssValue(“some property”)
获得特定CSS属性的值,但是我可以获得所有属性的值而不必明确提及它们的名称吗?

这在本机Selenium API中是不可能的

但使用Javascript,您可以: 您可以使用Seleniums的
JavascriptExecutor.executeScript功能来使用一些javascript支持

必要的js代码可以找到,并且(由@Mahsum Akbas提议)

下面是Java/Selenium代码,它将以“css-attribute01:value01;css-attribute02:value02;”的形式返回字符串 请注意,这将返回元素上的所有css属性

WebElement we = driver.findElement(By.tagName("div"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
String script = "var s = '';" +
                "var o = getComputedStyle(arguments[0]);" +
                "for(var i = 0; i < o.length; i++){" +
                "s+=o[i] + ':' + o.getPropertyValue(o[i])+';';}" + 
                "return s;";

System.out.println(executor.executeScript(script, we));
但是
这将只提供“内联样式”,而不是应用于元素的所有css样式。如果您一个接一个地运行两个版本并打印结果,您将看到巨大的差异。

使用上述脚本获取所有计算样式属性的Python版本:

from selenium import webdriver
from pprint import pprint
#require geckodriver in the directory where this script runs
driver = webdriver.Firefox()
driver.get('https://stackoverflow.com')

#Set the element object to the inbox messages icon
element = driver.find_element_by_xpath('//a[@title="Recent inbox messages"]')

#Get all of the style properties for this element into a dictionary
styleprops_dict = driver.execute_script('var items = {};'+
                                   'var compsty = getComputedStyle(arguments[0]);'+
                                    'var len = compsty.length;'+
                                    'for (index = 0; index < len; index++)'+
                                    '{items [compsty[index]] = compsty.getPropertyValue(compsty[index])};'+
                                    'return items;', element)
pprint(styleprops_dict)
从selenium导入webdriver
从pprint导入pprint
#在运行此脚本的目录中需要geckodriver
driver=webdriver.Firefox()
司机,上车https://stackoverflow.com')
#将元素对象设置为收件箱消息图标
element=driver。通过xpath('//a[@title=“最近的收件箱消息”]”查找\u element\u
#将此元素的所有样式属性获取到字典中
styleprops\u dict=driver.execute\u脚本('var items={};'+
'var compsty=getComputedStyle(参数[0]);'+
'var len=compsty.length;'+
'for(index=0;index
也有类似的问题:实际上它只是有点类似,因为@Milad想知道如何在SeleniumWell中做到这一点,技术上你可以使用
driver.findElement(by.tagName(“div”)).getAttribute(“style”)
然后你必须像以前一样解析各个属性。@JeffC with getAttribute(“style”)您将只获得元素内联样式,而不是应用于元素的所有css样式!但这是一个很好的补充我的答案。thx。@ShubhamJain哈哈,thx伙计!给我带来1k xp;-)非常特别!
from selenium import webdriver
from pprint import pprint
#require geckodriver in the directory where this script runs
driver = webdriver.Firefox()
driver.get('https://stackoverflow.com')

#Set the element object to the inbox messages icon
element = driver.find_element_by_xpath('//a[@title="Recent inbox messages"]')

#Get all of the style properties for this element into a dictionary
styleprops_dict = driver.execute_script('var items = {};'+
                                   'var compsty = getComputedStyle(arguments[0]);'+
                                    'var len = compsty.length;'+
                                    'for (index = 0; index < len; index++)'+
                                    '{items [compsty[index]] = compsty.getPropertyValue(compsty[index])};'+
                                    'return items;', element)
pprint(styleprops_dict)