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

So I had a bit of trouble finding how to use JQuery to lookup stuff and pass it back to Selenium C#, bu

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744874149a4598446.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信