【发布时间】:2017-10-10 23:58:56
【问题描述】:
我正在尝试使用 selenium 单击网页源中的一些链接。这是我到目前为止得到的:
import selenium, time
import html5lib
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
pg_src = br.page_source.encode("utf")
soup = BeautifulSoup(pg_src)
br = webdriver.Chrome()
url = "http://somewikipage.org"
br.get(url)
lnkLst = soup.find_all("a", href=re.compile(",_California") # this builds a list with everything in the a href tag
nuLst = []
for i in lnkLst:
nuLst.append(i.get('href')) #this removes all the unclickable text from the a href tag
for i in nuLst:
br.find_element_by_link_text(i).click()
这会导致以下错误:
AttributeError: 'list' object has no attribute 'click'
我已经打印出 nuLst 并且每个项目都与 href 标记中的超链接完全匹配。在使用 find_element_by_xpath 之前我做了类似的事情,但我不确定如何在不调用页面上所有其他 href 的情况下隔离这组 href 的 css 选择器。
【问题讨论】:
-
我认为错误来自:br.find_element_by_link_text(i),参数:i 是链接的 url,而不是链接的文本。所以你应该将链接文本附加到 nuLst 而不是 href 处: nuLst.append(i.get('href'))
标签: python selenium beautifulsoup