首页 > 解决方案 > Selenium Webdriver 和 Jupyter Notebook。find_elements_by_xpath 不在同一个单元格中工作

问题描述

我正在运行下面的代码以从 wunderground.com 抓取一些数据。

lookup_URL = 'https://www.wunderground.com/hourly/us/ny/new-york-city/date/2020-07-28.html'

options = webdriver.ChromeOptions();
options.headless = False

driver = webdriver.Chrome('./chromedriver.exe', options = options)

driver.get(lookup_URL)
rows = driver.find_elements_by_xpath('//td[@class="mat-cell cdk-cell cdk-column-liquidPrecipitation mat-column-liquidPrecipitation ng-star-inserted"]')

如果我在单个单元格中运行该代码,我会得到一个空列表rows

但是,如果我rows = driver.find_elements_by_xpath(.......)之后在不同的单元格中运行这条线,我会得到我需要的数据。

问题是我需要一次执行脚本,而不是两部分。

有谁知道为什么会这样?非常感谢您的帮助!

标签: pythonseleniumselenium-webdriverweb-scrapingjupyter-notebook

解决方案


看来你需要等待。

rows = WebDriverWait(driver, 60).until(EC.visibility_of_all_elements_located((By.XPATH, 'xpath_here')))

导入后:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

显式等待


推荐阅读