I have a snippet of code that loops through links in a page using Selenium.
I have replaced Time.sleep(3)
with a WebDriverWait()
to try to speed it up (as I'm running through 50 pages) but I can't get it to work. For some reason implicitly_wait(30)
doesn't do the trick.
Without any Time.sleep()
or WebDriverWait()
the code generates a stale element reference error when trying to declare the variable "elems", as the driver can't find any elements by xpath - not surprising.
The WebDriverWait() that I've used here solves that problem, but then the code runs into another: On the last line of the nested for-loop, it throws another stale element reference error. It seems that although the elements are loaded when WebDriverWait() lets the code proceed, the attributes of these elements are not necessarily ready.
My objective is to make the code as fast as possible, so Time.sleep(10) or something similar wouldn't be very appealing. How do I smoothly make sure that all elements and their attributes are loaded before continuing? And why could it be that implicitly_wait(30) does not solve this issue?
EDIT: The URLs in question: From http://www.oddsportal.com/baseball/usa/mlb-2017/results/#/page/1/ to http://www.oddsportal.com/baseball/usa/mlb-2017/results/#/page/50/
PageRange = range(1, 50)
for page in PageRange:
URL = "http://www.oddsportal.com/baseball/usa/mlb-" + season + "/results/#/page/" + str(page) + "/"
driver.get(URL)
tryElement = EC.presence_of_element_located((By.XPATH, "//a[@href]"))
WebDriverWait(driver, 30).until(tryElement)
elems = driver.find_elements_by_xpath("//a[@href]")
for elem in elems:
#This line I can't get not to throw a stale element reference
link = elem.get_attribute("href")