The aim of my program is to enter a set of postcodes into a flatshare website (https://www.spareroom.co.uk/flatshare-house-share/uk/) and return the number of rooms wanted and rooms available for each postcode.
My problem is that after clicking a radio button (rooms for rent) the program is meant to re-enter the postcode and search for results after having done the same with the rooms wanted button. It works for the rooms wanted button, but when I reuse the same send_keys() command, it doesn't seem to work, generating the following error...
Traceback (most recent call last):
File "c:\Users\SS\Dropbox\PC\Programming\Python Programming\Web Automation\WebScraper.py", line 34, in <module>
search_box.send_keys(postcode, Keys.ENTER)
File "C:\Users\SS\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 303, in send_keys
self._execute(
File "C:\Users\SS\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 572, in _execute
return self._parent.execute(command, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\SS\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 448, in execute
self.error_handler.check_response(response)
File "C:\Users\SS\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 232, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: stale element not found
Below is a snippet of my code for this specific task:
wanted_button = driver.find_element(By.ID, "flatshare_type-wanted")
driver.execute_script("arguments[0].checked = true;", wanted_button)
search_box = driver.find_element(By.ID, "search_by_location_field")
print(wanted_button)
search_box.send_keys(postcode, Keys.ENTER)
time.sleep(5)
rent_button = driver.find_element(By.ID, "flatshare_type-offered")
driver.execute_script("arguments[0].checked = true;", rent_button)
search_box.send_keys(postcode, Keys.ENTER)
print(rent_button)
time.sleep(5)
Potentially useful background information:
I've had a number of errors such as...
- The
click();method was not working for the radio buttons so I tried to fix it using a Python version of theexecute_script()method that someone suggested in Java online - Sometimes the whole automation seems to close within a few seconds, even though it's not programmed to do so, but when I rerun it, without altering the code, it suddenly starts working again
- There's another error that continually pops up, before it even reaches this part of the program, because it comes before the error listed above. It didn't seem to be causing issues, but I'm not sure if it is affecting this part of the code for some reason. It looks like this...
DevTools listening on ws://127.0.0.1:57005/devtools/browser/1f515eee-5ae0-4407-a2bc-4d42cd7bb3e6
[34088:37684:0512/205949.001:ERROR:services\device\usb\usb_descriptors.cc:146] Failed to read length for configuration 1.
[34088:37684:0512/205949.037:ERROR:services\device\usb\usb_descriptors.cc:146] Failed to read length for configuration 2.
[34088:37684:0512/205949.037:ERROR:services\device\usb\usb_descriptors.cc:105] Failed to read all configuration descriptors. Expected 3, got 1.
[34088:37684:0512/205949.037:ERROR:components\device_event_log\device_event_log_impl.cc:202] [20:59:49.038] USB: usb_device_win.cc:95 Failed to read descriptors from \\?\usb#vid_0b95&pid_1790#00a226a5#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
I'm a beginner to Python, so any help would be greatly appreciated, thanks.