[英]How can get Child Nodes's List from parent Node's Attribute in C# xmldocument
[英]How to get attribute from child node through Selenium and C#
下面提到的是网页中的一个节点,目标是在属性“ onclick”内获取数据。 我知道我可以使用GetAttribute("onclick")
来获取数据。
但是出于某种原因,我只能找到该输入节点所在的td。 有人可以告诉是否有一种方法可以从父“ td”节点获取子“输入”节点的属性数据“ onclick”。
<td align="center">
<input type="button" class="button" value="View Pdf" onclick="showFilePreView('98374');">
</td>
如果使用selenium-webdriver,则可以先找到父元素,然后使用childElement = parentElemnent.FindElement(By.)
查找子元素。
您可以尝试以下代码:
IWebElement parentElement = YourWebDriver.FindElement(By.TagName("td"));
// those codes above assume that there is only one "td" node in your case.
IWebElement childElement = parentElement.FindElement(By.Name("button"));
String theStringYouWant = childElement.GetAttribute("onclick");
希望这些代码可以解决您的问题。
所需元素是动态元素,因此要获取属性onclick的值 ,即showFilePreView('98374') ,必须诱使WebDriverWait使该元素可见,并且可以使用以下任一解决方案:
CssSelector
:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("td[align='center']>input.button[value='View Pdf']"))).GetAttribute("onclick")
XPath
:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//td[@align='center']/input[@class='button' and @value='View Pdf']"))).GetAttribute("onclick");
注意 :这里假设<td>
节点在HTML DOM中是唯一的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.