Python-Selenium Webdriver陷入循环中的.get() [英] Python - selenium webdriver stuck at .get() in a loop

查看:370
本文介绍了Python-Selenium Webdriver陷入循环中的.get()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python代码段,该代码段使用Selenium Webdriver循环显示了一些历史性的Baseball赔率.该代码的第一部分旨在从时间表表中获取所有单独的游戏URL(由大约57个页面组成,需要循环播放)并将它们存储在列表中.

I have a Python code snippet that uses the Selenium Webdriver to loop through some historical Baseball odds. This first part of the code is intended to get all the individual Game URL's from the schedule table (consisting of around 57 pages that need to be looped) and store them in a list.

我第一次进行测试时,它工作得很好-现在,无论出于何种原因,driver.get()函数似乎都无法正常工作.发生的情况是,webdriver在pageRange循环(第2页)中启动了第一个.get()方法,但是在此之后,在循环的下一次迭代中,它被卡住了并且无法导航到第3页.没有错误消息或崩溃.

The first time I tested this it worked just fine - now, for whatever reason, the driver.get() function seems to not be working. What happens is that the webdriver initiates the first .get() method in the pageRange loop (page 2), but after that, in the next iteration of the loop it gets stuck and doesn't navigate to page 3. No error message or crash.

使用print()进行的一些手动错误检查表明,该代码的所有其他区域都工作正常.这个问题的潜在原因可能是什么?

Some manual error checking using print() indicates that all other areas of the code are doing fine. What could be the potential reasons for this issue?

如上所述,代码实际上在第一个.get()调用之后立即卡住,而不是在第二个.get()调用前被卡住.我还注意到,当遍历游戏URL时,.get()函数在代码中的稍后效果很好.由于某些原因,它特别是" http://www.oddsportal.com/baseball/usa/mlb-2017/results/#/page/2/,""

EDIT 1: The Code actually gets stuck immediately after the first .get() call and not before the second, as stated above. I also noticed that the .get() function works just well later in the code when looping through Game URL's. For some reason it is specifically the "http://www.oddsportal.com/baseball/usa/mlb-2017/results/#/page/2/", ""http://www.oddsportal.com/baseball/usa/mlb-2017/results/#/page/3/", etc, that it gets stuck on.

season = str(2017)

URL = "http://www.oddsportal.com/baseball/usa/mlb-" + season + "/results/#/"
chrome_path = r"C:\Users\dansl110\Dropbox\Betting Project/chromedriver.exe"

OddsList = pd.DataFrame(columns=["Date", "HomeTeam", "AwayTeam", "HomeOdds", 
"AwayOdds", "Accuracy"])

GameURLs = []
StartURL = 2

#Gets GameURLs and EndPage from Page 1
driver = webdriver.Chrome(chrome_path)
driver.get(URL)
elems = driver.find_elements_by_xpath("//a[@href]")
for elem in elems:
    link = elem.get_attribute("href")
    if "/results/#/page/" in link:
        EndURL = int(''.join(c for c in link if c in digits))
    elif "/mlb" in link and len(str(link)) > 58 and "results" not in link:
        GameURLs.append(link)

PageRange = range(StartURL, EndURL - 5)

#Gets remaining GameURLs
for page in PageRange:
    oldURL = URL
    URL = "http://www.oddsportal.com/baseball/usa/mlb-" + season + 
    "/results/#/page/" + str(page) + "/"
    #This .get() works only during the first iteration of the range loop
    driver.get(URL)
    time.sleep(3)
    elems = driver.find_elements_by_xpath("//a[@href]")
    for elem in elems:
        link = elem.get_attribute("href")
        if "/nhl" in link and len(str(link)) > 65 and "results" not in link:
            GameURLs.append(link)

推荐答案

从今天开始,我也遇到了同样的问题.我发现我运行过64.-版本的Chrome的所有计算机都出现间歇性的挂起问题,但是运行63.-的计算机却没有.转到chrome://settings/help并检查哪个版本:

I had this same problem starting today. What I found was any of the machines that I had running versions 64.- of Chrome were having an intermittent hanging issue, but the machines running 63.- were not. go to chrome://settings/help and check which version:

如果正在运行该版本.请尝试在此处(2.35)下载Chromedriver版本: https://sites.google. com/a/chromium.org/chromedriver/downloads

If you are running that version. try downloading the Chromedriver version here (2.35): https://sites.google.com/a/chromium.org/chromedriver/downloads

我尝试过这种方法,它似乎可以帮助解决挂起的问题,但似乎仍在发生.

I tried this and it seemed to help a little with the hanging, but it still seems to be occurring.

唯一修复此问题的方法是重新为Chrome构建63.-.

The only thing that fixed it is going back to build 63.- for Chrome.

希望它对您有帮助.

我发现线程将对您有所帮助!在创建驱动程序之前,将其添加到脚本中:

I found this thread that will help! Add this to your script before you create the driver:

from selenium import webdriver

ChromeOptions = webdriver.ChromeOptions()
ChromeOptions.add_argument('--disable-browser-side-navigation')
driver = webdriver.Chrome('your/path/to/chromedriver.exe', chrome_options=ChromeOptions)

一旦Chrome版本65.-出现,它将得到修复.同时,如果您仍在64.-

Once Chrome version 65.- comes out, it will be fixed. In the meantime, use the above if you are still on 64.-

这篇关于Python-Selenium Webdriver陷入循环中的.get()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆