簡體   English   中英

Selenium Webdriver 和 Jupyter Notebook。 find_elements_by_xpath 不在同一個單元格中工作

[英]Selenium Webdriver and Jupyter Notebook. find_elements_by_xpath not working in same cell

我正在運行下面的代碼以從 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(.......) ,我會得到我需要的數據。

問題是我需要一次執行腳本,而不是兩部分。

有誰知道為什么會這樣? 非常感謝您的幫助!

看來你需要等待。

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

顯式等待

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM