2010-10-05 109 views
4

我一直在环顾四周,试图找到一种方法来点击selenium中与正则表达式匹配的链接。在python selenium中点击regexp的链接

下面是可用的代码;

from selenium import selenium 
sel = selenium("localhost", 4444, "*chrome", "http://www.ncbi.nlm.nih.gov/") 
sel.start() 
sel.open('/pubmed') 
sel.type("search_term", "20032207[uid]") 
sel.click("search") 
sel.click("linkout-icon-unknown-vir_full") 

但是如果我在不同的ID搜索链接文本会有所不同,但它始终是正则表达式匹配linkout-icon[\w-_]*

但我似乎无法找到其点击匹配的正则表达式的链接的权利命令我已经试过:

sel.click('link=regex:linkout-icon[\w-_]*') 
sel.click('regex:linkout-icon[\w-_]*') 
sel.click('link=regexp:linkout-icon[\w-_]*') 
sel.click('regexp:linkout-icon[\w-_]*') 

但他们都不在所有的工作。有什么建议么?

编辑:

所以在下面的答案意见后:点击的项目实际上是用id = linkout图标 - 未知viro_full的图像。全线如下:

<a href="http://vir.sgmjournals.org/cgi/pmidlookup?view=long&amp;pmid=20032207" ref="PrId=3051&amp;itool=Abstract-def&amp;uid=20032207&amp;nlmid=0077340&amp;db=pubmed&amp;log$=linkouticon" target="_blank"><img alt="Click here to read" id="linkout-icon-unknown-vir_full" border="0" src="http://www.ncbi.nlm.nih.gov/corehtml/query/egifs/http:--highwire.stanford.edu-icons-externalservices-pubmed-standard-vir_full.gif" /></a> </div> 

如果你想知道我从Selenium IDE记录器得到的代码。

回答

2

sel.click可以将XPath作为参数。使用Firebug我发现(我相信这是)的XPath来“linkout图标 - 未知vir_full”链接:

sel.click("//*[@id='linkout-icon-unknown-vir_full']") 

使用上面的命令需要我this page


我是不是能够得到matches工作 - 我不知道为什么 - 但这似乎使用contains工作:

sel = selenium.selenium("localhost", 4444, "*firefox", "http://www.ncbi.nlm.nih.gov/") 
sel.start() 
sel.open('/pubmed') 
sel.type("search_term", "20032207[uid]") 
sel.click("search") 
sel.wait_for_page_to_load(30000) 
sel.click("//*[contains(@id,'linkout')]") 
+0

正确的想法,但我需要一个正则表达式,因为我要从搜索列表中获取这些。对于不同的搜索,我需要匹配不同的链接。 – JudoWill 2010-10-05 18:06:54

0

我觉得你非常接近。首先,regexp:是说您想要使用正则表达式的正确文本模式。

,可能是不完全正确的另一件事是说link=,因为这是指该链接的文本,即:

<a href="path/to/mylink">Text of the link, this is what will be searched</a> 

那么部分锚你想使用你的正则表达式,href?

的东西,可能导致朝着正确的答案是这样的:selenium: Is it possible to use the regexp in selenium locators

也许这让功能可以重新用于搜索所有a.href属性的正则表达式,然后返回他们每个人的XPath来,然后被输送到click()

0

后做一些黑客周围我想出可能是最愚蠢的方式来做到这一点,但它的工作原理,直到有人能给我提供一个更好的答案:

import re 
val = re.findall('linkout-icon-unknown[\w-]*', sel.get_html_source())[0] 
sel.click(val) 

它需要我去搜索整个HTML和可能拿出ISSU如果设计更改,则为es。

我很想看到一个更强大的方法。

相关问题