我正在尝试使用Selenium访问此网站上的“下载csv”按钮。https://fplreview.com/team-planner/#forecast_table。当我第一次单击该站点时,我需要输入一个“ Team ID”并单击“提交”,这很好,但是随后会出现一个弹出广告,我无法将其关闭。我已经尝试了几种主要使用XPATH的方法,但是它说即使我添加了一个睡眠计时器来等待页面加载等,按钮也不存在。我的最终目标是使用请求来执行此操作,但是我想使其正常运行首先使用 selenium 。谢谢。下面是我的代码
`
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://fplreview.com/team-planner/#forecast_table')
team_id = driver.find_element_by_name('TeamID')
team_id.send_keys(123)
team_id.submit()
# click close button on ad.
ad_path = '//*[@id="butt"]/html/body/div[2]/div[3]/div/div/div/div[1]/div/article/div[2]/div/div[18]/div/div/div[3]/button'
button = driver.find_element_by_xpath(ad_path)
button.click()
# export csv
export_button = driver.find_element_by_id('exportbutton')
export_button.click()
driver.quit()
`
产生的错误
`
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="butt"]/html/body/div[2]/div[3]/div/div/div/div[1]/div/article/div[2]/div/div[18]/div/div/div[3]/button"}
`
需要等待一些时间才能使其正常工作:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get('https://fplreview.com/team-planner/#forecast_table')
team_id = driver.find_element_by_name('TeamID')
team_id.send_keys(123)
team_id.submit()
# wait for the ad to load
WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.ID, 'orderModal_popop')))
# hide the ad
driver.execute_script("jQuery('#orderModal_popop').modal('hide');")
# export csv
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div[3]/div/div/div/div[1]/div/article/div[2]/div/button[6]')))
export_button = driver.find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div/div[1]/div/article/div[2]/div/button[6]')
export_button.click()
# wait for download
time.sleep(3)
driver.quit()