简体   繁体   English

如何使用Selenium WebDriver,NUnit和C#获取元素属性的子属性值

[英]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的getComputedStylegetPropertyValue来获取继承的样式属性值:

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: 要检索cssTextfontWeight ,可以使用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. 注意 :您需要将WebDriverWaitExpectedConditions一起作为ElementIsVisible方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用querySelector获取元素的value属性 - How to get the value property of an element using querySelector 无法读取未定义错误的属性“ setValue”。 如何使用Selenium WebDriver和Java在codeMirror元素中键入文本? - Cannot read property 'setValue' of undefined error. How to type text in codeMirror element using Selenium WebDriver and Java? 如何使用 Javascript Selenium C# 更改元素的值 - How to Change value of Element using Javascript Selenium C# 在Selenium Webdriver ruby​​脚本中获取元素值 - Get element value in Selenium webdriver ruby script Selenium WebDriver(节点js / C#)-元素不可见/尝试将值发送到搜索框时没有此类元素异常 - Selenium WebDriver (node js/ C#)-Element not visible/No such element exception while trying to send value to a search box 如何使用C#向下滚动Selenium WebDriver? - How to scroll down with Selenium WebDriver using C#? 带有 Python 的 Selenium Webdriver:元素不可点击且无法读取 null 的属性“点击” - Selenium Webdriver with Python: Element is not clickable & Cannot read property 'click' of null 如何使用C#在Selenium中使用JavaScriptExecutor将样式属性设置为div - How to set style property to div using JavaScriptExecutor in selenium using C# 在C#中使用Selenium WebDriver执行JavaScript - Execute JavaScript using Selenium WebDriver in C# 带有C#的VS中的Selenium Webdriver-使用Promise - Selenium Webdriver in VS with C# - Using promises
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM