[英]How to get attribute from child node through Selenium and C#
Below mentioned is a node in a webpage and the objective is to get the data inside the attribute "onclick". 下面提到的是网页中的一个节点,目标是在属性“ onclick”内获取数据。 I am aware that i can use GetAttribute("onclick")
to get the data. 我知道我可以使用GetAttribute("onclick")
来获取数据。
But for a reason I am only locating the td inside which this input node is present. 但是出于某种原因,我只能找到该输入节点所在的td。 Can someone tell if there is a way to get the attribute data "onclick" of the child 'input' node from parent 'td' node. 有人可以告诉是否有一种方法可以从父“ td”节点获取子“输入”节点的属性数据“ onclick”。
<td align="center">
<input type="button" class="button" value="View Pdf" onclick="showFilePreView('98374');">
</td>
If you use selenium-webdriver, then you could find the parent element first, then use childElement = parentElemnent.FindElement(By.)
to find the child. 如果使用selenium-webdriver,则可以先找到父元素,然后使用childElement = parentElemnent.FindElement(By.)
查找子元素。
In your case, you could try the code below: 您可以尝试以下代码:
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");
Hope those code could solve your case. 希望这些代码可以解决您的问题。
The desired element is an dynamic element so to get the value of the attribute onclick ie showFilePreView('98374') you have to induce WebDriverWait for the element to be visible and you can use either of the following solutions: 所需元素是动态元素,因此要获取属性onclick的值 ,即showFilePreView('98374') ,必须诱使WebDriverWait使该元素可见,并且可以使用以下任一解决方案:
CssSelector
: CssSelector
:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("td[align='center']>input.button[value='View Pdf']"))).GetAttribute("onclick")
XPath
: XPath
:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//td[@align='center']/input[@class='button' and @value='View Pdf']"))).GetAttribute("onclick");
Note : Here the assumption is the <td>
node is unique within the HTML DOM 注意 :这里假设<td>
节点在HTML DOM中是唯一的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.