Rob*_*nar 7 python python-2.7 selenium-webdriver
我对Seleniun WebDriver和Python很陌生,我的问题可能很基础.
所以,我有以下HTML代码:
<a class="wp-first-item" href="admin.php?page=account">Account</a>
Run Code Online (Sandbox Code Playgroud)
而且我想提取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
Run Code Online (Sandbox Code Playgroud)
要么
driver.find_element_by_xpath(".//*[@id='toplevel_page_menu']/ul/li[2]/a").href
Run Code Online (Sandbox Code Playgroud)
似乎没有工作,导致:
AttributeError: 'WebElement' object has no attribute 'link'
Run Code Online (Sandbox Code Playgroud)
我期待结果如此"admin.php?page=account"
.
Tha*_*Guy 12
你可以使用get_attribute
:
element = driver.find_element_by_xpath(".//*[@id='toplevel_page_menu']/ul/li[2]/a")
href = element.get_attribute('href')
print href
Run Code Online (Sandbox Code Playgroud)
通常我使用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']
Run Code Online (Sandbox Code Playgroud)
不幸的是,BeautifulSoup不支持xpath,所以上面是你的xpath的BS表示(据我所知).
归档时间: |
|
查看次数: |
7998 次 |
最近记录: |