I'm scraping a web with the following structure
Menu1
Submenu1
Event1
Event2
...
Submenu2
Event n
...
...
Menu2
....
Where to have access to Submenu you have to click on Menu, so as to expand the node and make submenu list visible, and to have access to Event list the same, you need to click on the corresponding submenu node. Once you get the event, you click on it and it goes to another page. After you scrape some, you go back to the previous page and scrape the next event. The code would be as follows
browser=webdriver.Chrome()
browser.get(url)
Menu1=browser.find_element_by_xpath('some expression')
Menu1.click()
submenu=Menu1.find_elements_by_xpath('some other expression')
for sub in submenu:
event=sub.find_elements_by_xpath('expression here')
for ev in event:
event.click()
Some scraping
'Go back to previous page'
browser.execute_script("window.history.go(-1)")
After doing the first iteration when trying to do the second for Event2, I get a NoSuchElementException, basically because after going back list are not expanded and therefore not all objects are visible to keep on with the for looping.
Is there any way that suits the code to fix this? My guess is that if I click Menu1
again, all Submenu elements will be visible so that list will be once again available. Afterwards, if I click on sub
then the Events list will be available as well.
Eager to read your suggestions Thanks