【问题标题】:How to get child property value of a element property using selenium webdriver, NUnit and C#如何使用 selenium webdriver、NUnit 和 C# 获取元素属性的子属性值
【发布时间】:2019-07-18 02:02:30
【问题描述】:

我正在使用 selenium webdriver 测试网站,但我在获取另一个属性的子属性的值时遇到了困难。对我来说,这个 2nd/child 级别总是返回为 null。

当尝试获取上层属性/属性的值时,它可以使用以下代码正常工作:

return Element1.GetAttribute("baseURI");
return Element2.GetAttribute("innerText");

上面的返回我期望的文本/字符串。但是,如果我尝试获取子属性的值,如下所示:

return Element3.GetAttribute("style.cssText");
return Element4.GetAttribute("style.fontWeight")

我得到空值。当我查看上面元素的 DOM/属性时,我确实看到了它们的值。

cssText: "font-weight: bold;"
fontWeight: "bold"

如果我在开发工具栏中右键单击属性并选择“复制属性路径”,我会得到以下信息:

style.cssText
style.fontWeight    

所以我认为问题在于我如何通过假设我从开发人员工具栏中复制的内容是正确的来引用子属性。我已经尝试过除句点之外的其他分隔符,但我现在仍然很幸运。

我正在尝试找出返回值的语法 -

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 的形式返回

【问题讨论】:

  • 这不是重复的(我不是要求所有值)并且共享的链接甚至没有得到批准的有利答案
  • 我正在尝试找出返回存储在 - object.style.fontWeight 中的值的语法:parent.child.GetCSSValue("css"), parent-child.GetCSSValue(" css") parent.child.GetCSSValue("css"), parent-child.GetCSSValue("css") 或: parent.child.GetAttribute("attrib"), parent-child.GetAttribute("attrib") parent.child .GetAttribute("attrib"), parent-child.GetAttribute("attrib") 甚至:parent.child.GetProperty("prop"), parent-child.GetProperty("prop") parent.child.GetProperty("prop "), parent-child.GetProperty("prop") 这些都返回为 null 或 empty.string

标签: javascript c# selenium getcomputedstyle getpropertyvalue


【解决方案1】:

看来你已经很接近了。要检索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);

注意:您需要将 WebDriverWaitExpectedConditions 一起诱导为 ElementIsVisible 方法。

【讨论】:

    【解决方案2】:

    您可以使用 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);
    

    有关getComputedStyle 的更多详细信息,您可以找到here。你可以在How to get all css-styles from a dom-element using Selenium, C# 中找到关于 css 和 selenium 的所有其他内容@

    【讨论】:

    • 感谢该链接,但我仍然想看看您将如何引用子属性。我会看看我是否可以在 .NET 中实现你所说的。
    猜你喜欢
    • 2018-11-03
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 2018-10-18
    • 2015-07-31
    相关资源
    最近更新 更多