【问题标题】:python selenium enter search query then waitpython selenium 输入搜索查询然后等待
【发布时间】:2018-08-31 18:12:38
【问题描述】:
actions = ActionChains(driver)
actions.send_keys(search_query + Keys.ENTER)
actions.perform()
# code to wait until page loads
src = driver.page_source

我将如何实现这一点?我正在尝试将密钥发送到我拥有的搜索框,然后在 .perform 之后我希望它等到搜索结果加载,然后获取源代码。硒有可能吗?

试图找到比time.sleep(2)更好的方法,也许像driver.wait_until_page_loads()这样的方法

【问题讨论】:

    标签: python selenium web-scraping


    【解决方案1】:

    我希望它等到搜索结果加载

    您正在寻找的是WebDriverWait(又名“显式等待”)。

    您想选择一个表示结果已加载的特定元素并等待。假设您正在等待一个元素的存在,该元素可以通过mySearchResultsid 定位。代码如下所示。

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    # create driver and execute search here
    
    # now wait for the presence of the `mySearchResults` element.
    # if it is not found within 10 secs, a `TimeOutException` is raised.
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "mySearchResults")))
    

    【讨论】:

    • 谢谢!这正是我需要的:)
    猜你喜欢
    • 2020-06-29
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 2021-09-27
    • 2012-04-07
    • 1970-01-01
    • 2018-02-04
    • 2018-08-09
    相关资源
    最近更新 更多