代码之家  ›  专栏  ›  技术社区  ›  Sheshank S.

请输入搜索查询,然后等待

  •  2
  • Sheshank S.  · 技术社区  · 6 年前
    actions = ActionChains(driver)
    actions.send_keys(search_query + Keys.ENTER)
    actions.perform()
    # code to wait until page loads
    src = driver.page_source
    

    我将如何实现这一点?我正在尝试发送关键字到搜索框,我有,然后在执行。我希望它等到搜索结果加载,然后得到源代码。硒有可能吗?

    想找到一个比 time.sleep(2) driver.wait_until_page_loads()

    1 回复  |  直到 6 年前
        1
  •  4
  •   Corey Goldberg    6 年前

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

    WebDriverWait (又称“显式等待”)。

    您需要选择一个表示结果已加载的特定元素,然后等待。假设您正在等待可以通过 id 属于 mySearchResults . 代码如下所示。

    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")))