Python Selenium:无法获取表内容

2024-05-15 09:05:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试访问thisURL,在这里我必须获取价格/税收历史部分下的表。下面是我的代码:

from selenium import webdriver
from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
import os, sys
from multiprocessing import Pool
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Firefox()
wait = WebDriverWait(driver, 5)
driver.maximize_window()
driver.get('https://www.zillow.com/homedetails/2114-Bigelow-Ave-N-Seattle-WA-98109/48749425_zpid/')
sleep(10)
p_history = driver.find_elements_by_css_selector('#tax-price-history  table tr > td')
    for p in p_history:
        print(p.text)

它不是打印文本。你知道吗

更新所需部分的屏幕:

enter image description here

更新#2

在PhantomJS上运行,在这里您可以看到加载程序部分中的图像(滚动图像)

enter image description here


Tags: from图像importuisupportbydriverselenium
1条回答
网友
1楼 · 发布于 2024-05-15 09:05:20

您需要告诉selenium使用WebDriverWaitexpected_conditions来查找加载后的元素。你知道吗

您需要一个对页面加载中不存在的元素的引用,但是一旦Ajax请求完成,该元素就应该存在。看起来#tax-price-history table应该满足这个要求。你知道吗

尝试:

from selenium.webdriver.support import expected_conditions as EC
parent = wait.until(EC.presence_of_element_located((
    By.CSS_SELECTOR, '#tax-price-history table')))

p_history = parent.find_element_by_css_selector('td')

如果在wait中指定的时间限制内找不到元素,则会出现错误

相关问题 更多 >