python selenium进入搜索查询然后等待

问题描述 投票:2回答:1
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个回答
3
投票

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

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

您希望选择表示结果已加载的特定元素并等待该元素。假设您正在等待可以使用idmySearchResults定位的元素的存在。代码看起来像这样。

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")))
© www.soinside.com 2019 - 2024. All rights reserved.