How to get a HtmlElement by Id

2019-09-05 13:08发布

问题:

Need to find a HtmlElement by Id (coded-ui). I got a BrowserWindow. Can find a specific element (table, cell, button, etc.), but not just an arbitrary element by its Id. E.g., the following h3 (in real life the h3 is part of complex table cell. We don't need the cell, we just need the header).

<html>
<body>
   <h3 id='check'>Check this text!</h3>
</body>
</html>

Anybody an idea how to do this? Thanks in advace! Gerard

回答1:

After you've recorded the steps you desire on your [TestMethod], you should have sub-methods in the form

[TestMethod]
public void MyTestMethod()
{
    this.UIMap.XXXX();
    this.UIMap.YYYY();
    this.UIMap.ZZZZ();
} 

Click on a sub-method with your mouse and press F12, that should open the UIMap.Designer.cs in which you can see the recorded steps..

Now just right click on the control you want and expand the Search Criteria.

Depending on the type of element it would look something like this :

this.mUITboxXXXX.SearchProperties[HtmlEdit.PropertyNames.Id] = "Replace here with what you want"

Edit : Example of a crosshair on text inside an h3 tag:



回答2:

Onilol suggested the following (which works fine as the id is fixed and we just need the text):

var browser = BrowserWindow.Launch("http://www.oursite.check/");
object txt = browser.ExecuteScript("return $('#check').text()", null);