Selecting and Identifying element with JQuery to use it in Selenium 2 Java API

后端 未结 3 1067
时光说笑
时光说笑 2020-12-09 14:02

This is the situation:

  • I use the Java API of Selenium 2 to open and control a firefox browser instance
  • I load the jQuery script to a page via JavaScri
3条回答
  •  没有蜡笔的小新
    2020-12-09 14:29

    I found the solution, which is quite easy:

    String jQuerySelector = "'#myDiv input.test'";
    RenderedWebElement webElement = (RenderedWebElement) ((JavascriptExecutor) webDriver).executeScript("return $(" + jQuerySelector+ ").get(0);");
    

    Working with an element in jQuery which was previosly selected in Selenium works too:

    String jQuerySelector = "arguments[0]";
    ((JavascriptExecutor) webDriver).executeScript("return $(" + jQuerySelector+ ").doSomethingInJquery();", webElement);
    

提交回复
热议问题