Home > Enterprise >  python scrollIntoView to the element error
python scrollIntoView to the element error

Time:10-20

I am trying to get names of teams by ID. With action of to move focus to element I get element names to list and text files. At some point web page reloads, and screen freezes then it stopes focus move and does not take team names to the list or text file neither. I even tried time.sleep(3) it could not get any team name data any.

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from datetime import datetime

driver = webdriver.Chrome(r"C:\Users\Admin\Downloads\chromedriver_win32 (1)\chromedriver.exe")
driver.get("https://www.nba.com/schedule?pd=false&region=1")
driver.implicitly_wait(5)
element_to_click=driver.find_element(By.ID,"onetrust-accept-btn-handler") #.click()
element_to_click.click()
element_to_save=driver.find_element(By.XPATH,"//div/div/div/div/h4")

f=open('new_result_file00.txt','w')#before optional read=write mode was ,r ,
f.write(element_to_save.text)
f.write("\n")
f.write(str(datetime.today()))
myList=[]
myList.append(1)

elements_to_save=driver.find_elements(By.XPATH,"//*[@data-id='nba:schedule:main:team:link']")
i=1
for element in elements_to_save:
    driver.execute_script("arguments[0].scrollIntoView();", element)
    try:
        f.write(element.text)
        myList.append(element.text)
    except Exception as e:
        print("err",i)
        i=i 1
    f.write(" \n ")
    f.write(str(datetime.today()))

f.close()

error TraceBack:

err 1
Traceback (most recent call last):
  File "C:\pythonPro\w_crawl\w01_nba.py", line 23, in <module>
    driver.execute_script("arguments[0].scrollIntoView();", element)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 491, in execute_script
    return self.execute(command, {
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 428, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=105.0.5195.127)
Stacktrace:
Backtrace:
    Ordinal0 [0x004FDF13 2219795]
    Ordinal0 [0x00492841 1779777]
    Ordinal0 [0x003A423D 803389]
    Ordinal0 [0x003A6D04 814340]
    Ordinal0 [0x003A6BC2 814018]
    Ordinal0 [0x003A755F 816479]
    Ordinal0 [0x003FFC1B 1178651]
    Ordinal0 [0x003EE7FC 1107964]
    Ordinal0 [0x003FF192 1175954]
    Ordinal0 [0x003EE616 1107478]
    Ordinal0 [0x003C7F89 950153]
    Ordinal0 [0x003C8F56 954198]
    GetHandleVerifier [0x007F2CB2 3040210]
    GetHandleVerifier [0x007E2BB4 2974420]
    GetHandleVerifier [0x00596A0A 565546]
    GetHandleVerifier [0x00595680 560544]
    Ordinal0 [0x00499A5C 1808988]
    Ordinal0 [0x0049E3A8 1827752]
    Ordinal0 [0x0049E495 1827989]
    Ordinal0 [0x004A80A4 1867940]
    BaseThreadInitThunk [0x75B8FA29 25]
    RtlGetAppContainerNamedObjectPath [0x77357B5E 286]
    RtlGetAppContainerNamedObjectPath [0x77357B2E 238]


Process finished with exit code 1

CodePudding user response:

I added expected conditions and waitwebdriver package and put 10sec to wait until element loads (!with 5 sec error ) everything flied off

wait = WebDriverWait(driver, 10)
elements_to_save=wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@data-id='nba:schedule:main:team:link']")))
  • Related