How to get a HtmlElement by Id

若如初见. 提交于 2019-12-12 01:21:57

问题


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


来源:https://stackoverflow.com/questions/33996333/how-to-get-a-htmlelement-by-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!