【问题标题】:Stuck in a for loop - no "next button"陷入 for 循环 - 没有“下一个按钮”
【发布时间】:2019-06-25 12:15:32
【问题描述】:

我正在尝试在 Rotogrinder/Resultdb 上搜索所有在特定日期参加比赛的团队。但是,我刚刚意识到,当正在抓取的比赛少于 50 个条目(只有一页)时,我的代码会停留在循环中,因为我通常使用下一步按钮退出该循环。这是我的代码:

*** 如果出现问题,我改三行直接去比赛。

#Packages:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import pandas as pd
import time
from selenium.common.exceptions import NoSuchElementException


# Driver
chromedriver =("C:/Users/Michel/Desktop/python/package/chromedriver_win32/chromedriver.exe")
driver = webdriver.Chrome(chromedriver)

# DF taht will be use later 
results = pd.DataFrame()
best_lineups=pd.DataFrame()
opti_lineups=pd.DataFrame()

#For loop over all DATES:

CALENDAR=[]
CALENDAR.append("2017-10-04")
CALENDAR.append("2017-10-05")



for d in CALENDAR:
    driver.get("https://rotogrinders.com/resultsdb/date/"+d+"/sport/4/")
   
    time.sleep(10)
    
    
    
    try:
        
        contest= driver.find_element_by_xpath("//*[@id='root']/div/main/main/div[2]/div[3]/div/div/div[1]/div/div/div/div/div[3]")
    
  

        contest.click()
        list_links = driver.find_elements_by_tag_name('a')
        hlink=[]
        for ii in list_links:
            hlink.append(ii.get_attribute("href"))
        sub="https://rotogrinders.com/resultsdb"
        con= "contest"
        contest_list=[]
        for text in hlink:
            if sub in text:
                if con in text:
                    contest_list.append(text)
                    
# comment next two lines and replace by following three to get directly to where the code keep looping                   

#         for c in contest_list:
#             driver.get(c)
       
        c=contest_list[3:5]
        for a in c:
            
            driver.get(a)
   
        
            n=1
            while n<100:
  

                n+=1
                time.sleep(10)
        
#       
                try:    
                    Data=driver.find_element_by_xpath('.//tbody//tr//td//span//a[text() != ""]').is_displayed()
        
                    next_button = driver.find_elements_by_xpath("//button[@type='button']")           
               
# Get tables to get the user names
                    tables=[]
                    tables = pd.read_html(driver.page_source)
                    users_df  = tables[0][['Rank','User']]
                    users_df['User'] = users_df['User'].str.replace(' Member', '')
                    users_df['order of appearence'] = users_df.groupby('User')['User'].transform(lambda x : x.duplicated().cumsum().add(1))
            # Initialize results dataframe and iterate through users
      
                    for i, row in users_df.iterrows():
                        rank = row['Rank']
                        user = row['User']
                    
                        count_IP= users_df["order of appearence"][i]-1
                
                                        
    # Find the user name and click on the name
                        user_link = driver.find_elements(By.XPATH, "//a[text()='%s']" %(user))[count_IP]
                        user_link.click()

    # Get the lineup table after clicking on the user name
                        tables = pd.read_html(driver.page_source)
                        lineup = tables[1]

    # Restructure to put into resutls dataframe
                        lineup.loc[9, 'Name'] = lineup.iloc[9]['Salary']
                        lineup.loc[10, 'Name'] = lineup.iloc[9]['Pts']

                        temp_df = pd.DataFrame(lineup['Name'].values.reshape(-1, 11), 
                        columns=lineup['Pos'].iloc[:9].tolist() + ['Total_$', 'Total_Pts'] )
    
                        temp_df.insert(loc=0, column = 'User', value = user)
                        temp_df.insert(loc=0, column = 'Rank', value = rank)
                        temp_df["Date"]=d
                        results = results.append(temp_df)  
    
                    
                    next_button[2].click()    
                    results = results.reset_index(drop=True)
    
                except NoSuchElementException: 
                    break
    except NoSuchElementException: 
        pass      
        

driver.close()

我尝试在循环末尾添加以下内容,但没有成功:

 try:
    next_button = driver.find_elements_by_xpath("//button[@type='button']")
                        
    next_button[2].click()    
 except NoSuchElementException:
     break    ***( I also try with pass) 

我该如何解决这个问题?

【问题讨论】:

  • 请发个链接

标签: python selenium for-loop web-scraping


【解决方案1】:

尝试使用webElement.isEnabled()webElement.isDisplayed() 检查按钮是否启用。这是关于可点击元素的post

祝你好运!

【讨论】:

  • 问题是按钮不可点击。我的目标是停止循环,而按钮不可点击,因为只有一页。否则,我会不断地报废同一张桌子......
  • 你不能检查它是否可以点击,如果不是,请停止循环?
  • 这是@Todor 建议的,但它不起作用。就像按钮不可点击,但没有引发异常......
【解决方案2】:
try:
    next_button =  driver.find_elements_by_xpath("//button[@type='button']")
    next_button[2].click()    
 except NoSuchElementException:
    n = 100
    break

你的页面控制变量是n,设置为100,while循环就会停止。

【讨论】:

  • 它不工作。我仍然陷入困境。感觉还是可以找到next_button,也没有引发NoSuchElementException……所以不要进入except部分
  • 好吧,如果找到它,循环将在第 100 次出现时停止 - while 将启动,那里没有 2 个选项。您确定这不是运行时间缓慢的情况吗?在循环开始时打印n 的值,以查看您在重复中的位置。
猜你喜欢
  • 1970-01-01
  • 2015-07-28
  • 1970-01-01
  • 2020-04-10
  • 2019-07-09
  • 1970-01-01
  • 2021-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多