navigate a table in selenium
Suppose I have a simple table, like this:
Smith | Select
Jones | Select
Johnson | Select
And I need to write a Selenium test to select the link corresponding to Jones. I can make it click the "Select" in the 2nd row, but I would rather have it find where "Jones" is and click the corresponding "Select". I'm thinking I may need to incorporate JQuery to a开发者_JAVA百科ccomplish this?
Use an xpath expression like "//tc[.//text() = 'Jones']/../tc[2]/a" which looks for a table cell whose text contents are 'Jones' and then naviagates up to that cells parent, selects the second table cell of that parent, and then a link inside the second table cell.
If you wanna use jQuery, here is some information:
- 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 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
精彩评论