【发布时间】:2019-06-09 02:04:17
【问题描述】:
无法弄清楚如何单击此页面表格底部的“下一步”按钮:
https://www.zacks.com/stocks/industry-rank/reit-and-equity-trust-other-266/stocks-in-industry
这是我尝试过的:
from bs4 import BeautifulSoup
import requests
import csv, random, time
from pandas.io.html import read_html
from selenium import webdriver
from selenium.webdriver.support.ui import Select
url = 'https://www.zacks.com/stocks/industry-rank/reit-and-equity-trust-other-266/stocks-in-industry'
# Open Chrome
driver = webdriver.Chrome()
# Send Chrome to the URL
page = driver.get(url)
# Wait for page to load a few seconds
timeDelay = random.randrange(4, 8)
time.sleep(timeDelay)
# Try to click the darn button
element = driver.find_element_by_xpath('//*[@id="industry_rank_table_next"]')
driver.execute_script("arguments[0].click();", element)
...和
element = driver.find_element_by_xpath('//*[@id="industry_rank_table_next"]')
element.send_keys("\n")
...从其他答案中找到但对我不起作用。简单地使用.click() 是行不通的。我还尝试使用css_selector、partial_link_text 和class_name 选择按钮,但仍然没有成功。我在几个网站上遇到过这个。有什么想法吗?
【问题讨论】:
-
试试
element.click()? -
即使按钮被另一个元素覆盖(我猜是页面底部的 Cookies 警告)
driver.execute_script("arguments[0].click();", element)仍然可以工作。你有什么例外? -
@Andersson 有趣的是,代码点击链接成功,但是我得到一个“selenium.common.exceptions.WebDriverException: Message: unknown error: call function result missing 'value'”错误,所以我将添加一个 try/except 选项来处理错误。我要更新了。感谢您的帮助!
标签: python-3.x selenium xpath css-selectors webdriverwait