2017-05-07 30 views
0

私はChrome webdriverでPython/Seleniumを使用していますが、別の<td>の内容に基づいて<td>からURLを取得しようとしています。私のマークアップは次のようになります。jQueryとスクリプトの実行者と十分に簡単ですJSからPython Seleniumへの文字列の受け渡し

<div class="targetclass"> 
    <tr> 
     <td><a href="[email protected]">emailval2</a></td> 
     <td><a href="[email protected]">emailval</a></td> 
    </tr> 
</div> 

:私はwebdriverを一緒に使用するために返されたHREFを保存しようとすると、しかし

with open('jquery-3.2.1.min.js', 'r') as jquery_js: 
    jquery = jquery_js.read() #read the jquery from a file 
    driver.execute_script(jquery) # activate the jquery lib 
    driver.execute_script("$('div.targetclass a[href$=\"[email protected]\"]').parents(\"tr\").find(\"a:first\").attr('href')") 

、私は次の結果を持っている:

aurlval = driver.execute_script("$('div.targetclass a[href$=\"[email protected]\"]').parents(\"tr\").find(\"a:first\").attr('href')") 
print (aurlval) 

返される値は

None 
です

ターゲットURL([email protected])を保存して、Webdriverで操作できるようにするにはどうすればよいですか?

答えて

1

セレンとの私の経験は、私はいくつかの自動化を望んでいた(こするために、私は通常の要求とBeautifulSoupとによって得ることができます)が、私はexecute_scriptが戻らないので、あなたがなしを取得されていない理由があると信じて、いくつかのニッチな例に制限されています(あなたのスクリプトは基本的にウェブページに注入され、ブラウザ内で実行されます)。 IIRC、あなたは(冗長に)にあなたのjQueryを解析することができるはずです。

div = driver.find_element_by_class_name("targetclass") 
targeta = div.find_element_by_link_text("[email protected]") 
tr = targeta.parent.parent 
retrieve = tr.find_element_by_tag_name("a") 
aurlval = retrieve.getattribute("href") 

セレンは、最初の要素対リストに別々のメソッドを持っている場合、あなたが持つかもしれないので、私は、私の頭の上を思い出すことができませんそれらの行でゼロインデックスを取る。

+0

私は.parent行でロードブロッキングを打っています.Seleniumでは.parentが異なる方法で使用されていると思います。これを実現するにはxpathが必要なようです。 – Marcatectura

関連する問題