假设我有一个简单的表,如下所示:
Smith | Select
Jones | Select
Johnson | Select
我需要编写一个Selenium测试来选择对应于Jones的链接。我可以让它点击第二行的"Select“,但我宁愿让它找到"Jones”在哪里,然后点击相应的"Select“。我在想,我可能需要整合JQuery来实现这一点?
发布于 2010-06-23 03:47:57
使用类似"//tc.// text () = 'Jones'/../tc2/a“这样的xpath表达式,该表达式查找文本内容为'Jones‘的表格单元格,然后导航到该单元格父单元格,选择该父单元格的第二个表格单元格,然后选择第二个表格单元格中的链接。
发布于 2012-08-16 01:25:11
如果你想使用jQuery,这里有一些信息:
首先,你可以从jquery.js或jquery.min.js文件中读取jquery。
下面是一些代码:
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
https://stackoverflow.com/questions/2225263
复制相似问题