等待Selenium WebDriver for python

时间:2019-02-14 00:51:36

标签: python selenium wait

我尝试在https://www.fantasycruncher.com/lineup-rewind/draftkings/NHL/2019-02-03

上产生一晚的1000个最佳团队

但是,每次迭代只能生成500个团队。因此,我需要单击按钮Calculate 500 more teams,但是一旦浏览器生成了前500个团队,该按钮就会停止运行。因此,我需要等待浏览器完成生成团队,然后将按钮从显示的待处理Calculate 500 more teams按钮切换回Stop

我试图等待Calculate按钮出现:

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消息,指出该按钮在某个时刻是不可单击的……查看元素面板(通过单击“检查”),我感觉Calculate 500 more teams按钮始终存在,但是有警告,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”,因此{{1 }}按钮切换到Stop

1 个答案:

答案 0 :(得分:2)

正如您正确说过的那样,两个按钮(“停止”和“计算更多的团队”)始终是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