【发布时间】:2020-10-11 12:36:03
【问题描述】:
我想使用 selenium 进行搜索,然后在 DDG 搜索结束时单击“更多结果”按钮。
当显示查询的所有结果时,DDG 搜索不再显示按钮。
我想在没有按钮的情况下退出try循环。
我将分享我现在正在尝试的内容。我之前也尝试过这两个选项:If len(button_element) > 0: button_element.click(),我尝试过If button_element is not None: button_element.click()。
我想要使用 Selenium 的解决方案,以便它显示浏览器,因为它有助于调试
这是我的代码,带有可重现的示例:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
browser = webdriver.Chrome()
browser.get("https://duckduckgo.com/")
search = browser.find_element_by_name('q')
search.send_keys("this is a search" + Keys.RETURN)
html = browser.page_source
try:
button_element = browser.find_element_by_class_name('result--more__btn')
try:
button_element.click()
except SystemExit:
print("No more pages")
except:
pass
【问题讨论】:
-
这对我来说非常适合codeshare.io/5oZlep
标签: python selenium beautifulsoup webdriverwait duckduckgo