How to use JQuery in Selenium?
You can try using my selenium lib at github.
It handles almost the entire jquery API minus the functions that use/require handler passing:
HtmlUnitDriver drv = new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6);drv.setJavascriptEnabled(true);try { jQueryFactory jq = new jQueryFactory(); jq.setJs(drv); drv.get("http://google.com"); jq.query("[name=q]").val("SeleniumJQuery").parents("form:first").submit(); String results = jq.queryUntil("#resultStats:contains(results)").text(); System.out.println(results.split(" ")[1] + " results found!");} finally { drv.close();}
Since you said that you didn't have an ID but a class:
(only class-ID provided)
...a better answer is likely to use the CSS locator strategy which is already baked-in to Selenium where you can select an element based on a css class or simply by using CSS selector logic (for at least css2 and css3)
So to select an element (div, span whatever) that has a specific class you can simply use this for the Selenium locator:
css=.class-ID
You can even use more complicated selectors that are similar to those available in JQuery such as:
css=#myDiv .class-ID
This will search for the element with a css style of class-ID
within the element with an ID = myDiv
.
- First you can read the jquery from a jquery.js or jquery.min.js file.
- Then using execute_script(jquery) to enable jquery dynamically.
- Now you can interact with jquery.
here is some code:
browser = webdriver.Firefox() # Get local session of firefoxwith 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 themjs = """ 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: When I use Selenium to fetch some content from some website, they always ban me. Now you should use some proxy to go over it.
here is some code:
PROXY_HOST = "127.0.0.1"PROXY_PORT = 8087SOCKS_PORT = 8088fp = webdriver.FirefoxProfile()# Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5fp.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 desiredbrowser= webdriver.Firefox(firefox_profile=fp) # with proxybrowser = webdriver.Firefox() # no proxybrowser.get("http://search.example.com") # Load pageelem = browser.find_element_by_id("query_box") # Find the query inputelem.send_keys(u'my query string') # send query string to the inputelem.submit() # submit the query form