Python Forum
Multiple for loops selenium webdriver
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple for loops selenium webdriver
#1
I have a code which looks this:
for elem in browser.find_elements_by_xpath('/html/body/form[2]/table/tbody/tr[1]/td[1]/select/option'):
  elem.click()
  sleep(2)
  for ele in browser.find_elements_by_xpath('/html/body/form[2]/table/tbody/tr[1]/td[2]/table/tbody/tr[1]/td[1]/select/option'):
    ele.click()
It shows up an error of Stale Element. This code is just a sample - I have multiple of these nested for loops.

What should I do?
Reply
#2
why is this in a loop at all?
what is the URL?
Reply
#3
StaleElementReferenceException Traceback (most recent call last)
<ipython-input-4-68ac917a3735> in <module>()
2 elem.click()
3 for ele in second_op:
----> 4 ele.click()
5
6 browser.find_element_by_xpath('/html/body/form[2]/table/tbody/tr[1]/td[2]/table/tbody/tr[2]/td/a').click()

c:\python27\lib\site-packages\selenium\webdriver\remote\webelement.pyc in click(self)
78 def click(self):
79 """Clicks the element."""
---> 80 self._execute(Command.CLICK_ELEMENT)
81
82 def submit(self):

c:\python27\lib\site-packages\selenium\webdriver\remote\webelement.pyc in _execute(self, command, params)
631 params = {}
632 params['id'] = self._id
--> 633 return self._parent.execute(command, params)
634
635 def find_element(self, by=By.ID, value=None):

c:\python27\lib\site-packages\selenium\webdriver\remote\webdriver.pyc in execute(self, driver_command, params)
319 response = self.command_executor.execute(driver_command, params)
320 if response:
--> 321 self.error_handler.check_response(response)
322 response['value'] = self._unwrap_value(
323 response.get('value', None))

c:\python27\lib\site-packages\selenium\webdriver\remote\errorhandler.pyc in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):

StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

it is a census data. I need to run through all list of communities, within that each province, municipality and so on.
Reply
#4
Please answer question, why loops.
Reply
#5
I need loops because I need to go through multiple lists..

it is a census data. I need to run through all list of communities, within that each province, municipality and so on.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fixture not returning webdriver element Nik1811 1 176 Apr-15-2024, 04:39 PM
Last Post: Nik1811
  Selenium Webdriver Python — ERR_NO_SUPPORTED_PROXIES TimofeyKolpakov 0 1,777 May-06-2023, 02:13 PM
Last Post: TimofeyKolpakov
  WebDriver' object has no attribute 'find_element_by_css_selector rickadams 3 5,900 Sep-19-2022, 06:11 PM
Last Post: Larz60+
  webdriver does not work wiki11 2 1,749 Dec-06-2021, 10:52 AM
Last Post: wiki11
  Multiple loops Paulman 4 1,855 Oct-11-2021, 08:25 AM
Last Post: Paulman
  Multiprocessing using on multiple for loops Staph 3 2,567 Jul-28-2019, 07:24 AM
Last Post: ThomasL

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020