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

标签 python selenium web-scraping

actions = ActionChains(driver)
actions.send_keys(search_query + Keys.ENTER)
actions.perform()
# code to wait until page loads
src = driver.page_source

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

试图找到一种比 time.sleep(2) 更好的方法可能类似于 driver.wait_until_page_loads()

最佳答案

I would like it to wait till the search results load

您正在寻找的是 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")))

关于python selenium 输入搜索查询然后等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52120798/

相关文章:

python - 复杂的注释和聚合查询

Python根据第一项从二维数组中删除元素

java - 如何使用selenium web driver java点击一个包含某些单词作为文本的href

python - scrapy : find element which has particular text

python - 运行使用 scrapy 和 selenium 创建的解析器时出现问题

python - 我在从 scrapy 蜘蛛下载/抓取图像时遇到值错误,我正在使用图像管道

python - tensorflow 通过随机因素调整图像大小

java - "PageFactory.initElements(driver, this);"如何处理WebElements列表?

javascript - Protractor 中的嵌套页面对象

python - 如何反序列化 django rest 框架中的多个对象列表?