如何向Selenium Remote Control添加JQuery定位器

我一直很喜欢使用带有Selenium的XPath,甚至使用带有Javascript的getEval,但是一位同事说能够在Selenium中使用JQuery选择器并不是很好。

我用谷歌搜索了它,但找不到任何似乎对我有用的文章。 任何人都可以提供有关如何使用JQuery语法从selenium中提取doc元素及其各自值的全面指南。

我正在使用C#来编写我的selenium测试,所以如果有任何一个例子可以来自C#的观点,那就太棒了。

谢谢

Karl Swedberg写了一篇很棒的博客文章,可以在http://www.learningjquery.com/2009/04/better-stronger-safer-jquerify-bookmarklet上找到。

我们对此进行了调整,基本上在Selenium Server jar文件中我们修改了RemoteRunner.html以包含jquery JavaScript(从http://code.jquery.com/jquery-latest.min.js获得):

  

然后为了在Selenium中使用它,我们添加了位置策略:

 mySelenium.addLocationStrategy("jquery", "var loc = locator; " + "var attr = null; " + "var isattr = false; " + "var inx = locator.lastIndexOf('@'); " + "if (inx != -1){ " + " loc = locator.substring(0, inx); " + " attr = locator.substring(inx + 1); " + " isattr = true; " + "} " + "var found = jQuery(inDocument).find(loc); " + "if (found.length >= 1) { " + " if (isattr) { " + " return found[0].getAttribute(attr); " + " } else { " + " return found[0]; " + " } " + "} else { " + " return null; " + "}" ); 

注意上面添加的定位器策略是在Java中,但它只是一个字符串,所以应该很容易在C#中复制。 JQuery确实使事情变得更快,尤其是在Internet Explorer中!

要修改jar,您可以使用java命令行工具更新下载的selenium服务器jar。 创建一个与名为“core”的jar相同级别的文件夹,并将修改后的RemoteRunner.html和jquery.min.js文件放在那里。 然后运行如下:

 jar -uf selenium-server-standalone-2.0b3-APT.jar core\RemoteRunner.html jar -uf selenium-server-standalone-2.0b3-APT.jar core\jquery.min.js 

如果jar不在您的路径中,您可以使用完整路径,例如在Windows上,您可以使用以下内容执行它:

 "C:\Program Files\Java\jdk1.6.0_22\bin\jar.exe"  

您需要使用AddLocationStrategy方法定义新的位置策略,并且需要在user-extensions.js文件中包含jQuery。

你可以阅读和execute_script来启用jQuery:

  • 首先,您可以从jquery.js或jquery.min.js文件中读取jquery。
  • 然后使用execute_script(jquery)动态启用jquery。
  • 现在你可以与jquery交互了。

这里是python中的一些代码,其他语言类似:

 browser = webdriver.Firefox() # Get local session of firefox with open('jquery.min.js', 'r') as jquery_js: #read the jquery from a file jquery = jquery_js.read() browser.execute_script(jquery) #active the jquery lib #now you can write some jquery code then execute_script them js = """ var str = "div#myPager table a:[href=\\"javascript:__doPostBack('myPager','%s')\\"]" console.log(str) var $next_anchor = $(str); if ($next_anchor.length) { return $next_anchor.get(0).click(); //do click and redirect } else { return false; }""" % str(25) success = browser.execute_script(js) if success == False: break 

PS:当我使用Selenium从某个网站获取某些内容时,他们总是禁止我。 现在你应该使用一些代理来完成它。
这是一些代码:

 PROXY_HOST = "127.0.0.1" PROXY_PORT = 8087 SOCKS_PORT = 8088 fp = webdriver.FirefoxProfile() # Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5 fp.set_preference("network.proxy.type", 1) fp.set_preference("network.proxy.http", PROXY_HOST) fp.set_preference("network.proxy.http_port", PROXY_PORT) fp.set_preference("network.proxy.socks", PROXY_HOST) fp.set_preference("network.proxy.socks_port", SOCKS_PORT) fp.set_preference("network.proxy.ftp", PROXY_HOST) fp.set_preference("network.proxy.ftp_port", PROXY_PORT) fp.set_preference("network.proxy.ssl", PROXY_HOST) fp.set_preference("network.proxy.ssl_port", PROXY_PORT) fp.set_preference("network.proxy.no_proxies_on", "") # set this value as desired browser= webdriver.Firefox(firefox_profile=fp) # with proxy browser = webdriver.Firefox() # no proxy browser.get("http://search.example.com") # Load page elem = browser.find_element_by_id("query_box") # Find the query input elem.send_keys(u'my query string') # send query string to the input elem.submit() # submit the query form