Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.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 如何使用SeleniumWebDriver、NUnit和C获取元素属性的子属性值#_Javascript_C#_Selenium_Getcomputedstyle_Getpropertyvalue - Fatal编程技术网

Javascript 如何使用SeleniumWebDriver、NUnit和C获取元素属性的子属性值#

Javascript 如何使用SeleniumWebDriver、NUnit和C获取元素属性的子属性值#,javascript,c#,selenium,getcomputedstyle,getpropertyvalue,Javascript,C#,Selenium,Getcomputedstyle,Getpropertyvalue,我正在使用SeleniumWebDriver测试网站,但我很难获得另一个属性的子属性的值。对我来说,第二级/子级总是返回为空 当尝试获取上层属性/属性的值时,使用以下代码可以正常工作: return Element1.GetAttribute("baseURI"); return Element2.GetAttribute("innerText"); 上面这些返回我期望的文本/字符串。但是,如果我尝试获取子属性的值,如下所示: return Element3.GetAttribute("sty

我正在使用SeleniumWebDriver测试网站,但我很难获得另一个属性的子属性的值。对我来说,第二级/子级总是返回为空

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

return Element1.GetAttribute("baseURI");
return Element2.GetAttribute("innerText");
上面这些返回我期望的文本/字符串。但是,如果我尝试获取子属性的值,如下所示:

return Element3.GetAttribute("style.cssText");
return Element4.GetAttribute("style.fontWeight")
style.cssText
style.fontWeight    
我正在变空。当我查看上面元素的DOM/属性时,我确实看到了它们的值

cssText: "font-weight: bold;"
fontWeight: "bold"
如果我右键单击开发人员工具栏中的属性并选择“复制属性路径”,我会得到以下结果:

return Element3.GetAttribute("style.cssText");
return Element4.GetAttribute("style.fontWeight")
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或空。string

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

有关
getComputedStyle
的更多详细信息,请参见。你在

中找到的关于css和selenium的其他内容似乎都非常接近。要检索
cssText
fontwweight
,您可以使用,然后使用检索样式,您可以使用以下解决方案:

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

注意:您需要将WebDriverWait与as方法结合起来。

此方法的可能重复项不是重复项(我没有要求所有值),并且共享的链接甚至没有一个有利的批准答案。我正在尝试找出返回存储在-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.GetProperty(“prop”)、parent-child.GetProperty(“prop”)parent.child.GetProperty(“prop”)、parent-child.GetProperty(“prop”)这些都返回为null或空。string感谢您的链接,但我仍然希望看到如何引用子属性。我会看看是否能在.NET中实现您所说的内容。