我正在为 Motley Fool 网站编写网络抓取工具。该站点有一个
load more articles
按钮。该按钮是可点击的,直到所有文章都加载完毕然后消失。使用我的代码可以找到并启用按钮,但无法单击。我已经使用了xpath
,name
,css selector
和更多/更少具体的xpaths
希望它变得可点击但似乎没有任何效果。
这是我的这个功能的代码片段:
url=https://www.fool.com/quote/nasdaq/aep/
driver.get(url)
wait = WebDriverWait(driver, 20)
try:
while(driver.find element(By.XPATH,'/html/body/div[8]/div[2]/div[1]/section[2]/div/div[1]/div[2]/div[4]/div[3]/div[1]/button').is displayed()):
element=driver.find_element(By.XPATH,'/html/body/div[8]/div[2]/div[1]/section[2]/div/div[1]/div[2]/div[4]/div[3]/d iv[1]/button')
driver.execute_script("arguments[0].scrollIntoView();", element)
button = wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[8]/div[2]/div[1]/section[2]/div/div[1]/div[2]/div[4]/div[3]/div[1]/button')))
print(button.text)
print(button.is_enabled())
driver.execute_script("arguments[0].click()", button)
time.sleep(3)
except:
pass
任何想法将不胜感激。谢谢!
试试这个
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
url = "https://www.fool.com/quote/nasdaq/aep/"
driver.get(url)
wait = WebDriverWait(driver, 20)
viewMoreButton = '//span[text()="View More AEP Articles"]'
try:
while (driver.find_element(By.XPATH,
viewMoreButton).is_displayed()):
element = driver.find_element(By.XPATH,
viewMoreButton)
driver.execute_script("arguments[0].scrollIntoView();", element)
button = wait.until(EC.element_to_be_clickable(
(By.XPATH, viewMoreButton)))
print(button.text)
print(button.is_enabled())
driver.execute_script("arguments[0].click()", button)
time.sleep(3)
except:
pass