[英]How to get child property value of a element property using selenium webdriver, NUnit and C#
I am testing websites using selenium webdriver and I am having difficulties getting the value of a property that is a child of another property. 我正在使用Selenium WebDriver测试网站,但是我很难获得作为另一个财产的子财产的财产的价值。 For me, this 2nd/child level is always coming back as null.
对我来说,这个2nd / child级别总是返回null。
When trying to get a value of an upper level attribute/property it is working fine with the following code: 尝试获取上层属性/属性的值时,可以使用以下代码正常工作:
return Element1.GetAttribute("baseURI");
return Element2.GetAttribute("innerText");
Those above return the text/string that I am expecting. 上面的那些返回了我期望的文本/字符串。 However if I try and get the value of a child property like the following:
但是,如果我尝试获取子属性的值,如下所示:
return Element3.GetAttribute("style.cssText");
return Element4.GetAttribute("style.fontWeight")
I am getting null. 我越来越空了。 When I view the DOM/properties of the elements above, I do see the values that they have.
当我查看上述元素的DOM /属性时,确实看到了它们具有的值。
cssText: "font-weight: bold;"
fontWeight: "bold"
If I right click on the properties from within the Developer Toolbar and choose "Copy Property Path", I get the following: 如果我在开发人员工具栏中右键单击属性,然后选择“复制属性路径”,则会得到以下信息:
style.cssText
style.fontWeight
So I believe the problem is how I am referring to the child property by assuming what I am copying from the developer toolbar is correct. 因此,我认为问题在于,通过假设我从开发人员工具栏中复制的内容正确,我是如何引用子属性的。 I have tried other delimiters other than a period, but I am still having now luck.
除句号外,我还尝试了其他定界符,但现在我仍然很幸运。
I'm trying to figure out the syntax to return the value stored in - 我试图弄清楚返回存储在-的值的语法-
object.style.fontWeight
I've tried: 我试过了:
parent.child.GetCSSValue("css"), parent-child.GetCSSValue("css")
parent.child.GetAttribute("attrib"), parent-child.GetAttribute("attrib")
parent.child.GetProperty("prop"), parent-child.GetProperty("prop")
These all come back as null or empty.string 这些都以null或empty.string的形式返回
You can use JavaScript's getComputedStyle
and getPropertyValue
to get inherited style attribute value: 您可以使用JavaScript的
getComputedStyle
和getPropertyValue
来获取继承的样式属性值:
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string fontWeight = (string) js.ExecuteScript("return window.getComputedStyle(arguments[0]).getPropertyValue('fontWeight')", element);
string cssText = (string) js.ExecuteScript("return window.getComputedStyle(arguments[0]).cssText", element);
More details about getComputedStyle
you can find here . 有关
getComputedStyle
更多详细信息,您可以在这里找到。 Everything else about css and selenium you can find in How to get all css-styles from a dom-element using Selenium, C# 您可以在如何使用Selenium,C#从dom元素中获取所有CSS样式中找到有关CSS和Selenium的所有其他信息
Seems you were pretty close. 看来您已经很接近了。 To retrieve the
cssText
and fontWeight
you can use the getComputedStyle()
and then use getPropertyValue()
to retrieve the style and you can use the following solution: 要检索
cssText
和fontWeight
,可以使用getComputedStyle()
,然后使用getPropertyValue()
来检索样式,并且可以使用以下解决方案:
IJavascriptExecutor jse = (IJavascriptExecutor)driver;
String cssText_script = "var x = getComputedStyle(arguments[0]);" +
"window.document.defaultView.getComputedStyle(x,null).getPropertyValue('cssText');"; ";
String fontWeight_script = "var x = getComputedStyle(arguments[0]);" +
"window.document.defaultView.getComputedStyle(x,null).getPropertyValue('fontWeight');"; ";
string myCssText = (string) jse.ExecuteScript(cssText_script, Element3);
string myFontWeight = (string) jse.ExecuteScript(fontWeight_script, Element4);
Note : You need to induce WebDriverWait along with the ExpectedConditions as ElementIsVisible method. 注意 :您需要将WebDriverWait和ExpectedConditions一起作为ElementIsVisible方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.