質問
次のような単純なテーブルがあるとします。
Smith | Select
Jones | Select
Johnson | Select
そして、Jones に対応するリンクを選択するための Selenium テストを作成する必要があります。2 行目の「選択」をクリックさせることもできますが、「Jones」がどこにあるかを見つけて、対応する「選択」をクリックさせたいと思います。これを実現するには JQuery を組み込む必要があるのではないかと考えています。
解決
は、テキストコンテンツ、次いで「ジョーンズ」であり、表のセルを探している「[2] / [.//テキスト()= 'ジョーンズ] /../ TC // TC」のようなXPath式を使用しその細胞の親までnaviagatesは、その親の第2のテーブルのセル、及び第2の表のセル内のリンクを選択します。
他のヒント
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
所属していません StackOverflow