2013-03-13 283 views
5

我对Seleniun WebDriver和Python很新,我的问题可能是基本的。使用Selenium Webdriver和Python从XPath中提取链接?

所以,我有以下的HTML代码:

<a class="wp-first-item" href="admin.php?page=account">Account</a> 

而且我试图提取HREF出它,是XPath的手段,知道它的XPath是".//*[@id='toplevel_page_menu']/ul/li[2]/a"

我该怎么做?

driver.find_element_by_xpath(".//*[@id='toplevel_page_menu']/ul/li[2]/a").link 

driver.find_element_by_xpath(".//*[@id='toplevel_page_menu']/ul/li[2]/a").href 

好像不工作,会导致:

AttributeError: 'WebElement' object has no attribute 'link' 

我期待的结果是一样"admin.php?page=account"

回答

6

你可以使用get_attribute

element = driver.find_element_by_xpath(".//*[@id='toplevel_page_menu']/ul/li[2]/a") 
href = element.get_attribute('href') 
print href 

按说我用Selenium导航到一个页面,检索源和BeautifulSoup解析它:

from BeautifulSoup import BeautifulSoup 

# On the current page 
source = driver.page_source 
soup = BeautifulSoup(source) 

href = soup('<the tag containing the anchor>',{'id':'toplevel_page_menu'})[0]('ul')[0]('li')[2]('a')[0]['href'] 

不幸的是,BeautifulSoup不支持的XPath,所以上面是你的xpath的BS表示(据我了解)。

+0

我是否需要导入一些奇特的东西才能让get_attribute()工作? 在最后添加/ @ href似乎不起作用。 – 2013-03-13 15:08:19

+0

尝试'element = driver.find_element_by_xpath(“.//*[@ id ='toplevel_page_menu']/ul/li [2]/a”)'然后使用'get_attribute':'print element.get_attribute('href') '。这可能会起作用。我很抱歉,我通常不会通过Selenium提取源数据。就像我说的,我通常使用BS。 – That1Guy 2013-03-13 15:52:24

相关问题