Python Selenium点击Zillow评论分页按钮失败问题求助
解决Zillow评论分页点击异常的思路
首先明确:浏览器控制台的AWSALB SameSite警告只是未来兼容性提示,不是当前点击报错的原因,不用浪费时间在手动修改这个cookie上。以下是具体解决方向:
1. 精准定位评论区的分页按钮
你当前用find_elements取最后一个"Next page"按钮,可能误选中了页面其他区域的同类型按钮。直接用更精准的XPath锁定评论区的分页按钮(可根据实际页面的容器类名调整):
# 替换原按钮定位代码 wait = WebDriverWait(driver, 20) next_review_button = wait.until(EC.presence_of_element_located( (By.XPATH, "//div[contains(@class, 'review-list-container')]//button[@title='Next page']") ))
2. 模拟真实用户的点击行为
Zillow的前端可能对直接的click()有拦截,改用ActionChains模拟鼠标移动+点击,同时确保按钮在可视区域:
from selenium.webdriver.common.action_chains import ActionChains # 滚动到按钮居中位置 driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", next_review_button) # 等待按钮可交互 wait.until(EC.element_to_be_clickable(next_review_button)) # 模拟真实点击动作 ActionChains(driver).move_to_element(next_review_button).click().perform()
3. 绕过反爬检测
Zillow有严格的反爬机制,普通Selenium容易被识别。改用undetected-chromedriver(针对Chrome)或配置Firefox的隐身模式+自定义User-Agent:
- 使用undetected-chromedriver的示例:
import undetected_chromedriver as uc from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By url = "https://www.zillow.com/profile/AGENTPROFILE" driver = uc.Chrome() driver.get(url) next_review_button = wait.until(EC.element_to_be_clickable( (By.XPATH, "//div[contains(@class, 'review-list-container')]//button[@title='Next page']") )) ActionChains(driver).move_to_element(next_review_button).click().perform()
4. 检查登录状态
部分Zillow评论分页需要登录才能访问,尝试:
- 手动登录一次后导出浏览器cookie,在程序中加载:
import json # 先打开Zillow首页 driver.get("https://www.zillow.com") # 加载提前保存的登录cookie(假设已保存到cookie.json) with open("cookie.json", "r") as f: cookies = json.load(f) for cookie in cookies: driver.add_cookie(cookie) driver.refresh()
- 或者模拟完整登录流程,输入账号密码后再操作分页按钮。
内容的提问来源于stack exchange,提问作者Cody Stanley
相关产品推荐
相关产品推荐

