我尝试在一个晚上产生1000个最好的球队:https://www.fantasycruncher.com/lineup-rewind/draftkings/NHL/2019-02-03
但是,每次迭代只能生成500个团队。因此,我需要点击按钮Calculate 500 more teams
,但是一旦浏览器生成前500个团队,该按钮就会停止。因此,我需要等待浏览器完成生成团队,然后按钮从显示的待处理Calculate 500 more teams
按钮切换回Stop
。
我试着等到Calculate
button出现时:
WebDriverWait(driver, 1000).until(EC.presence_of_element_located((By.CLASS_NAME, "calc-more-teams")))
或者是可点击的:
calulatemorebutton=WebDriverWait(driver, 10000).until(EC.element_to_be_clickable((By.CLASS_NAME, "calc-more-teams")))
但是,我总是得到一个WebDriverException消息,该按钮在点处无法点击...查看元素面板(通过单击inspect),我觉得Calculate 500 more teams
button始终存在,但是有一个警报,Stop
,显示在浏览器正在产生团队,因此我应该采用不同的方式。我试图等到警报没有显示,但没有成功。
这是代码工作,但只有很少的团队生成。代码正在运行,因为我添加了一些time.sleep()
。但是,如果你增加了生成的团队数量,webDriverwait
将是必要的,代码将无法工作......
from selenium import webdriver
import csv
from selenium.webdriver.support.ui import Select
from datetime import date, timedelta
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.common.keys import Keys
chromedriver =("C:/Users/Michel/Desktop/python/package/chromedriver_win32/chromedriver.exe")
driver = webdriver.Chrome(chromedriver)
driver.get("https://www.fantasycruncher.com/lineup-rewind/draftkings/NHL/2019-02-03")
time.sleep(10)
closeButton = driver.find_element_by_class_name('close-login-alert')
closeButton.click()
# Generate lineups
Calculate_button = driver.find_element_by_id('calc-team')
select = Select(driver.find_element_by_id('select-objective'))
select.select_by_value("Actual_Pts")
lineups_textbox = driver.find_element_by_id('numOfLineups')
lineups_textbox.send_keys("10")
Calculate_button.click()
time.sleep(10)
closeButton2 = driver.find_element_by_class_name('swal2-confirm')
closeButton2.click()
# Calculate 500 more
more = driver.find_element_by_class_name("run-results")
idid=more.get_attribute("id")
realid=idid+"-slider-input"
moremore=driver.find_element_by_id(realid)
moremore.clear()
moremore.send_keys("5")
time.sleep(10)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "calc-more-teams")))
calulatemorebutton=driver.find_element_by_class_name("calc-more-teams")
calulatemorebutton.click()
# # download csv
WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH , '//div[@data-action="downloadPlayerlist"]')))
download_button = driver.find_element_by_class_name(' export-csv-dropdown')
download_button.click()
csv= driver.find_element_by_xpath("//div[@data-optid='export']")
csv.click()
time.sleep(5)
driver.close()
我希望有1000个团队生成并下载相应的CSV文件,但我无法通过等待浏览器生成第一组500个团队来生成“500”,因此Stop
按钮切换到Calulate more
。
正如您所说,两个按钮(“停止”和“计算#更多团队”)始终是DOM的一部分,但通常通过将“样式”属性保持为display:none
来隐藏“停止”按钮。我们可以用它来识别你的病情。
点击“计算500多个团队”按钮后,您可以调用此按钮等待“停止”按钮消失。
from selenium.common.exceptions import TimeoutException
try:
WebDriverWait(driver, 60).until(EC.invisibility_of_element_located((By.CSS_SELECTOR, ".button.expand.stop-calc.alert")))
except TimeoutException:
print("Did not load in time")
else:
#download_your_csv