I cannot save a simple column in the database. Scraping is okay. Print output is okay. Immediately after the scrape printing, an error message appears and the database entry fails. In fact, all those names should be inserted in a (vertical) column of the database. One (1) column only. What am I wrong for saving in the database?
driver.get("link 1")
driver.close
Example1=driver.find_element_by_class_name("teamHeader__name")
print(Example1.text)
driver.get("link 2")
driver.close
Example2=driver.find_element_by_class_name("teamHeader__name")
print(Example2.text)
driver.get("link 3")
driver.close
Example3=driver.find_element_by_class_name("teamHeader__name")
print(Example3.text)
#INSERT IN DATABASE
con = sqlite3.connect('/home/mypc/Desktop/aaaaaaaa/Database.db')
cursor = con.cursor()
records_added_Risultati = 0
Values = (Example1.text, Example2.text, Example3.text)
sqlite_insert_query = 'INSERT INTO TableExample (AllExamples) VALUES (?);'
count = cursor.execute(sqlite_insert_query, Values)
con.commit()
print("Record inserted successfully ", cursor.rowcount)
records_added_Risultati = records_added_Risultati + 1
cursor.close()
The error:
Traceback (most recent call last):
File "/home/mypc/Desktop/aaaaaaaa/Progetto.py", line 192, in <module>
Values = (Example1.text, Example2.text, Example3.text)
File "/home/mypc/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 76, in text
return self._execute(Command.GET_ELEMENT_TEXT)['value']
File "/home/mypc/.local/lib/python3.8/site-packages /selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/home/mypc/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/mypc/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: The element reference of [object String] "{\"element-6066-11e4-a52e-4f735466cecf\":\"ddc55fe3-1fe4-4560- bcf5-62d18fbeae63\"}" is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed
UPLOAD 1`
Scraping is okay. Print output is okay.
#Serie A
driver.get("link: For peace of mind, I prefer not to write the link")
driver.close
SerieA=driver.find_element_by_class_name("teamHeader__name")
SerieA_text = SerieA.text
print(SerieA.text)
#Serie B
driver.get("link: For peace of mind, I prefer not to write the link")
driver.close
SerieB=driver.find_element_by_class_name("teamHeader__name")
SerieB_text = SerieB.text
print(SerieB.text)
#Serie C - Gir A
driver.get("link: For peace of mind, I prefer not to write the link")
driver.close
SerieC_A=driver.find_element_by_class_name("teamHeader__name")
SerieC_A_text = SerieC_A.text
print(SerieC_A.text)
and 8 others the same ...
### INSERT IN DATABASE ###
con = sqlite3.connect('/home/mypc/Scrivania/aaaaa/Database.db')
cursor = con.cursor()
records_added_Risultati = 0
Values = ((SerieA_text), (SerieB_text), (SerieC_A_text), (SerieC_B_text), (SerieC_C_text), (SerieD_I_text), (SerieD_H_text), (PremierLeague_text), (Championship_text), (Liga_text), (Bundesliga_text), (Ligue1_text))
sqlite_insert_query = 'INSERT INTO ARCHIVIO_Campionati (Nome_Campionato) VALUES (?);'
count = cursor.executemany(sqlite_insert_query, Values) #executemany, no execute
con.commit()
print("Record inserted successfully ", cursor.rowcount)
records_added_Risultati = records_added_Risultati + 1
cursor.close()