如何使用Selenium WebDriver,NUnit和C#获取元素属性的子属性值 [英] How to get child property value of a element property using selenium webdriver, NUnit and C#

查看:282
本文介绍了如何使用Selenium WebDriver,NUnit和C#获取元素属性的子属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium WebDriver测试网站,但是我很难获得作为另一个财产的子财产的财产的价值.对我来说,这个2nd/child级别总是返回为null.

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. For me, this 2nd/child level is always coming back as 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")

我越来越空了.当我查看以上元素的DOM/属性时,确实看到了它们具有的值.

I am getting null. When I view the DOM/properties of the elements above, I do see the values that they have.

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

我尝试过:

parent.child.GetCSSValue("css"), parent-child.GetCSSValue("css")
parent.child.GetAttribute("attrib"), parent-child.GetAttribute("attrib")
parent.child.GetProperty("prop"), parent-child.GetProperty("prop")

这些全部返回为null或empty.string

These all come back as null or empty.string

推荐答案

您似乎很接近.要检索cssTextfontWeight,可以使用 getComputedStyle() ,然后使用 getPropertyValue() 检索样式,您可以使用以下解决方案:

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:

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);

注意:您需要与 ElementIsVisible 方法.

这篇关于如何使用Selenium WebDriver,NUnit和C#获取元素属性的子属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆