Python 哪一个与webDriver中BeautifulSoup的find(findTag,class=findValue)相同?

Python 哪一个与webDriver中BeautifulSoup的find(findTag,class=findValue)相同?,python,selenium,beautifulsoup,Python,Selenium,Beautifulsoup,我想使用与Selenium中BeautifulSoup相同的查找方法find(findClass,class_=findValue),我搜索了但没有找到我需要的,你能帮我吗?这就是你要找的: elements = [] classes = driver.find_elements_by_class(className) for c in classes: l = c.find_elements_by_tag_name(tagName) if l: elements

我想使用与Selenium中BeautifulSoup相同的查找方法find(findClass,class_=findValue),我搜索了但没有找到我需要的,你能帮我吗?

这就是你要找的:

elements = []
classes = driver.find_elements_by_class(className)
for c in classes:
    l = c.find_elements_by_tag_name(tagName)
    if l:
        elements.extent(l)
等于

soup.findAll("div", { "class" : className})

您只需将BeautifulSoup代码转换为功能强大的CSS选择器表达式,然后使用:


如果您需要执行一些任务,如单击按钮,请使用selenium API

如果需要从HTML代码中提取数据,应该使用BeautillSoup

您可以使用selenium获取HTML代码,然后使用BeautifulSoup对其进行解析:

html_source = browser.page_source
soup = BeautifulSoup(html_source)

对不起,我的原始代码是soup.find(“div”,class=“list container”).find_all(“a”),所以driver.find_elements_by_class(“list container”)可以吗?@mikezang您需要使用for循环来提取
a
。问题是我使用该结果来获取(“href”),但您的代码无法获取,错误是异常:“WebElement”对象没有属性“get”…@mikezang beautifulsoup和selenium web驱动程序不同。。。你不能只是“使用”selenium中beautifulsoup的方法。。。har07的解决方案应该可以得到结果。link=soup.find(“div”,class=“list container”)。find_all(“a”),href=link.get(“href”),上面的代码就可以了。但是你的两个答案都有相同的例外……正如Alex指出的,你需要学习Selenium的API,阅读文档。在selenium中,您在单个webelement上调用
get_属性(“href”)
,以获取属性值……我是通过get_属性获得的。谢谢你的建议@HAR07如何将这个soup.find(findTag,id=findValue)。find_all(linkTag)转换为driver.find_elements_by…?CSS选择器表达式为:
findTag#findValue linkTag
findTag[id=“findValue”]linkTag
。了解有关我的问题的更多信息是如何避免使用BeautifulSoup…,如果使用VeautifulSoup,我的代码可以:)
html_source = browser.page_source
soup = BeautifulSoup(html_source)