gpt4 book ai didi

python - 如何使用 Selenium 和 Python 处理 try 循环中的错误

转载 作者:行者123 更新时间:2023-12-04 09:37:31 25 4
gpt4 key购买 nike

我想使用 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

最佳答案

单击 末尾的更多结果按钮使用 Selenium 搜索结果WebDriver你要诱导WebDriverWait对于element_to_be_clickable()您可以使用以下任一Locator Strategies :

  • 代码块:
    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.keys import Keys
    from selenium.common.exceptions import TimeoutException

    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get('https://duckduckgo.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("this is a search" + Keys.RETURN)
    while True:
    try:
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.result--more__btn"))).click()
    print("Clicked on More Results button")
    except TimeoutException:
    print("No more More Results button")
    break
    driver.quit()
  • 控制台输出:
    Clicked on More Results button
    Clicked on More Results button
    Clicked on More Results button
    Clicked on More Results button
    Clicked on More Results button
    No more More Results button

  • You can find a relevant discussion in How to extract the text from the search results of duckduckgo using Selenium Python

    关于python - 如何使用 Selenium 和 Python 处理 try 循环中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62501050/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com