2017-05-07 164 views
0

我使用Python /硒与铬webdriver的,我试图找回根据其他<td>内容从一个<td>的URL。我的标记看起来像:传递字符串从JS到Python硒

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

这是很容易与jQuery和脚本执行:

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')") 

然而,当我尝试存储返回HREF与webdriver的使用,我有以下结果:

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

返回的值是

None 

如何存储目标网址([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") 

我不记得我的头顶,如果硒对名单VS第一要素不同的方法,所以你可能有在这些线上采取零指数。

+0

我打在.parent行路障,我想这是因为.parent在硒使用不同。它看起来像我可能需要xpath来实现这一点。 – Marcatectura