最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Selenium pass dom element to JQuery and get results back into C# - Stack Overflow

programmeradmin6浏览0评论

So I had a bit of trouble finding how to use JQuery to lookup stuff and pass it back to Selenium C#, but I got it working, so I thought I'd share what I've found.

In particular:

  • How to pass IWebElements from C# to JQuery?
  • How to get an array of objects back into C# from the results of the javascipt?

So I had a bit of trouble finding how to use JQuery to lookup stuff and pass it back to Selenium C#, but I got it working, so I thought I'd share what I've found.

In particular:

  • How to pass IWebElements from C# to JQuery?
  • How to get an array of objects back into C# from the results of the javascipt?
Share Improve this question asked Jun 28, 2016 at 20:55 frodo2975frodo2975 11.9k5 gold badges39 silver badges51 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Answering my own question here :)

You can pass webElements to the javascript and access them through arguments.

To get the results back into C#, you have to cast to <IReadOnlyCollection<IWebElement>>. For single elements, just use IWebElement. One trick for multiple results is you have to call toArray on the JQuery result, so that it returns a true array instead of an object.

var singleResult = Driver.ExecuteJavaScript<IWebElement>(
            "return $('.my-class')[0]");

IWebElement elementToPass = Driver.FindElement(By.Id("cool-id"));
var results = Driver.ExecuteJavaScript<IReadOnlyCollection<IWebElement>>(
            "return $(arguments[0]).children('.my-class').toArray()", elementToPass);
发布评论

评论列表(0)

  1. 暂无评论