2010-02-08 52 views
1

假設我有一個簡單的表,如下所示:瀏覽表中硒

Smith | Select 
Jones | Select 
Johnson | Select 

,我需要寫一個Selenium測試來選擇對應於瓊斯的鏈接。我可以讓它在第二行中點擊「選擇」,但我寧願讓它找到「瓊斯」在哪裏,然後單擊相應的「選擇」。我想我可能需要結合JQuery來完成這個任務?

回答

1

使用像「// tc [.// text()='Jones'] /../ tc [2]/a」這樣的xpath表達式來查找文本內容爲'Jones'的表格單元格和然後導航到該單元格父元素,選擇該父元素的第二個表格單元格,然後選擇第二個表格單元格內的鏈接。

0

如果你想使用jQuery,這裏是一些信息:

  • 首先,你可以閱讀從的jquery.js或jquery.min.js文件jQuery的。
  • 然後使用execute_script(jquery)來動態啓用jquery。
  • 現在你可以與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