-2

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()
  • 1
    What means "cannot save" here? What happens, what should happen? Show error messages (if any) as properly formatted text in the question. – Michael Butscher Jun 30 '21 at 03:09
  • @MichaelButscher I'm sorry. I am new here. I edited the question and added the error. Could you help me please? Thank you – Frederick Man Jun 30 '21 at 03:17
  • Instead of getting all the elements first and getting the text later, you could also store the text in a variable as soon as the element is obtained and later use that variable. I think that way you could avoid this stale element exception. – AKS Jun 30 '21 at 04:03
  • @AKS Could you show me with the code in an answer please? I don't think I understand. Thank you – Frederick Man Jun 30 '21 at 04:07
  • @FrederickMan I have added an answer as requested. Please check it. – AKS Jun 30 '21 at 04:13

1 Answers1

2

driver.get will load a new page. In the code, you are loading 3 pages and getting element from all of those. Once you load link 2 the Example1 element from the link 1 page gets stale. What you could do is that as soon as you get the element, you also get its text in a variable. And, do the same thing again for the next link.

driver.get("link 1")
driver.close
Example1=driver.find_element_by_class_name("teamHeader__name")
example1_text = Example1.text
print(Example1.text)

driver.get("link 2")
driver.close
Example2=driver.find_element_by_class_name("teamHeader__name")
example2_text = Example2.text
print(Example2.text)

driver.get("link 3")
driver.close
Example3=driver.find_element_by_class_name("teamHeader__name")
example3_text = Example3.text
print(Example3.text)

Later, instead of Values = (Example1.text, Example2.text, Example3.text), use the variables which have stored the text.

Values = ((example1_text,), (example2_text,), (example3_text,))

Please also note that each item in Values is a single row with column values. That is why each item here is a tuple. Also, you should use cursor.executemany because you are inserting multiple values in one go.

AKS
  • 18,983
  • 3
  • 43
  • 54
  • I thank you for the answer. The rest of my code, however, in #INSERT IN DATABASE .... is it correct? The problem was right there, in #INSERT IN DATABASE. Something was wrong there. If you gave me this kind answer, does it mean that my #INSERT IN DATABASE .... is correct? I needed that, #INSERT IN DATABASE – Frederick Man Jun 30 '21 at 04:21
  • 1
    @FrederickMan You are the best judge of whether it is correct or not. You should give it another try and at the end if you see that everything works without an error, it should be correct, right! – AKS Jun 30 '21 at 04:22
  • Thank you so much for your kindness. But now I get this error: count = cursor.execute (sqlite_insert_query, Values) sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 12 supplied (I used 12, but 3 of the example or 12 does the same). How can I solve? Thank you – Frederick Man Jun 30 '21 at 05:09
  • 1
    @FrederickMan please check the updated answer. It doesn't matter whether there are 3 entries or 12. Each item should be a tuple or list. Please see how `Values` should be created. – AKS Jun 30 '21 at 05:13
  • I had deleted and rewritten the comment again, sorry. In any case...i tried with your updated answer. I still have the same same mistake as above. Error: Incorrect number of bindings supplied. The current statement uses 1, and there are 12 supplied – Frederick Man Jun 30 '21 at 05:14
  • 1
    Ohh yes, you should use `cursor.executemany` instead of `cursor.execute`. – AKS Jun 30 '21 at 05:16
  • I still have the same error above, except that The current statement uses 1, and there are 7 supplied ..... and no longer are 12 supplied. I do not understand why. I entered 12, exactly as you kindly showed me. Strange mistake. Mah Do you have any ideas? – Frederick Man Jun 30 '21 at 05:35
  • 1
    Can you update your question with your new code? – AKS Jun 30 '21 at 05:36
  • 1
    @FrederickMan The way you are creating the `Values` is not correct `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))` Please notice that while you have added `()` around the values, those are still individual values and not tuple. You will notice that there is an extra comma just before closing `)` in the my answer to make it a tuple e.g. here it should be `Values = ((SerieA_text,), (SerieB_text,), .. and others`. – AKS Jun 30 '21 at 06:03
  • Ah, sorry. Here, now it's fine. Perfect. Thanks, you are great.A small question also for future readers please: I reproduced the same code, scraping no longer 1 single word for 1 site, but more words (with the same class) for 1 site. I also added the S to elements,but there is something wrong. I think it is a trivial and simple mistake. Could you tell me what's wrong please? (explain as simple as possible please, as I am new to Python). The code is the same as you suggested. I took a screenshot, thanks. ibb.co/8c4wQjv No scraping, no print.Then vote your answer and we finish. Thank you – Frederick Man Jul 01 '21 at 20:08
  • 1
    @FrederickMan I am not sure I fully understood your question. As always, just saying that there is something wrong doesn't make the question clear. In my opinion, we should not keep extending the scope of this POST. If you have a different query, please post a new question. – AKS Jul 02 '21 at 04:27
  • AKS Yes you are right. I posted a new question here, which is your kindly suggested code: https://stackoverflow.com/questions/68220412/problem-printing-the-grouped-data-list-after-scraping If you have time can you please help me? A small question to close this thread: what is the difference between Values ​​= ((SerieA_text,) and Values ​​= ((SerieA_text) without the comma after "text"? In this case, what is the function of the comma? Thanks – Frederick Man Jul 02 '21 at 06:18
  • 1
    @FrederickMan Please check out this [SO Post](https://stackoverflow.com/questions/12876177/how-to-create-a-tuple-with-only-one-element) to understand the difference. – AKS Jul 02 '21 at 06:23
  • OK thanks. I leave you this link again https://stackoverflow.com/questions/68220412/problem-printing-the-grouped-data-list-after-scraping in case you want to help me. It's almost the same code you wrote me. I am going crazy, I can not resolve. Thank you very much, you were kind. I appreciated. I already voted on your answer last time, now I put the green tick of the question resolved – Frederick Man Jul 02 '21 at 06:26