python调用selenium的get_Python – selenium webdriver在循环中停留在.get()
我有一個Python代碼片段,它使用Selenium Webdriver來循環一些歷史性的棒球賠率.代碼的第一部分旨在從調度表(包含大約57個需要循環的頁面)中獲取所有單獨的游戲URL,并將它們存儲在列表中.
我第一次測試它它工作得很好 – 現在,無論出于何種原因,driver.get()函數似乎無法正常工作.會發生什么事情是webdriver在pageRange循環(第2頁)中啟動第一個.get()方法,但之后,在循環的下一次迭代中它會卡住并且不會導航到第3頁.沒有錯誤消息或崩潰.
使用print()進行一些手動錯誤檢查表明代碼的所有其他區域都正常.可能是這個問題的潛在原因是什么?
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)
總結
以上是生活随笔為你收集整理的python调用selenium的get_Python – selenium webdriver在循环中停留在.get()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: crossin的编程教室python入门
- 下一篇: mysql通用日志不打印_解决logba