I'm trying to collect the URLs of all possible versions of this page (all combinations of Level, Event, and Season) using Selenium. I've been successful using driver.find_elements_by_xpath to navigate to the correct option and click it before saving the URL, but this has been very slow and I'm wondering if there's a better alternative.
There doesn't seem to be any href attribute I can steal the link from without clicking on the actual option. Using the Select class and trying to loop through the options is cleaner, but I still have to generate the Select object every time - trying to do this:
s = Select(driver.find_element_by_xpath("//label[contains(text(), 'Level')]/../select"))
for option in s.options:
option.click()
works for the first option, but then gives me the error stale element reference: element is not attached to the page document. I'm stumped - is there a better way to collect these links? Below is my snippet of code:
driver.get("https://athletic.net/TrackAndField/Division/Event.aspx?DivID=89120&Event=1")
for i in range(0, len(driver.find_elements_by_xpath("//label[contains(text(), 'Level')]/../select/option"))):
driver.find_elements_by_xpath("//label[contains(text(), 'Level')]/../select/option")[i].click()
for j in range(0, len(driver.find_elements_by_xpath("//optgroup//option[contains(text(), 'Meters')]"))):
driver.find_elements_by_xpath("//optgroup//option[contains(text(), 'Meters')]")[j].click()
for k in range(0, len(driver.find_elements_by_xpath("//label[contains(text(), 'Season')]/..//option[contains(text(), 'Indoor')]/../option"))):
driver.find_elements_by_xpath("//label[contains(text(), 'Season')]/..//option[contains(text(), 'Indoor')]/../option")[k].click()
for l in range(0, len(driver.find_elements_by_xpath("//label[contains(text(), 'Season')]/..//option[contains(text(), '2018')]/../option"))):
driver.find_elements_by_xpath("//label[contains(text(), 'Season')]/..//option[contains(text(), '2018')]/../option")[l].click()
with open("links.txt", 'a+') as f:
f.write(driver.current_url + ";")
<option value="12345">the event is the same thing.option. I'm not sure how to figure out what the DivID would be if I clicked on one of these options without actually clicking on it.