简体   繁体   English

如何通过Selenium和C#从子节点获取属性

[英]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.

相关问题 如何从C#XML文档中的父节点属性获取子节点列表 - How can get Child Nodes's List from parent Node's Attribute in C# xmldocument 如何通过Selenium和C#从元素获取文本 - How to Get the text from an element through Selenium and C# Selenium C#:如何获取Id属性? - Selenium c#: How to get the Id attribute? 如何在C#中从XDocument获取子节点元素的值 - How to get value of child node elements from XDocument in c# C#XML解析 - 获取子节点的属性 - C# XML parsing - Get attribute of child node 如何通过Xpath,C#和Selenium从元素内的文本节点中检索文本 - How to retrieve the text from a text node within an element through Xpath, C# and Selenium 如何在C#中将子元素属性值从xml获取到linq - how to get child element attribute value from xml to linq in c# 如何在C#中使用XmlDocument从具有属性的Element的子元素中获取XML信息? - How to get XML information from a child in an Element with a attribute using XmlDocument in C#? 如何使用C#从XML文件获取子属性值 - How to get child attribute value from XML file using c# 如何从子节点获取 C# 中的所有文本,但避免来自具有特定属性(例如已删除)的子节点的文本 - How to get all texts in C# from child nodes but avoid a text from those child that has specific attribute (such as removed)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM