Home > database >  why I am getting this error in pyhton for loop "stale element reference: element is not attache
why I am getting this error in pyhton for loop "stale element reference: element is not attache

Time:11-04

why I am getting this error in pyhton for loop "stale element reference: element is not attached to the page document"

here is my code

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('F:\\work\\chromedriver_win32\\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)

Content =["listtext1","listtext2","listtext3","listtext4"]
for i in range(4):
    time.sleep(7)
    url = "https://quillbot.com/"
    driver.get(url)
    Text_block = driver.find_element(By.ID, "inputText")
    Text_block.send_keys(Content[i])# Change (fetch from Search_list)
    time.sleep(2)

CodePudding user response:

Here is one way of sending those texts into that textbox, based on your existing code and how you defined waits:

[....]
content =["listtext1","listtext2","listtext3","listtext4"]

for i in content:
    driver.get('https://quillbot.com/')
    try:
        wait.until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))).click()
        print('accepted cookies')
    except Exception as e:
        print('no cookie button!')
    text_block = wait.until(EC.element_to_be_clickable((By.ID, "inputText")))
    text_block.send_keys(i)
    print('sent', i)
    t.sleep(5)

See Selenium documentation at https://www.selenium.dev/documentation/

CodePudding user response:

I made a few fixes to your code:

  1. Inserted user_agent (it will come in handy with other experiments on selenium)
  2. inserted a web driver manager to run selenium on all operating systems.
  3. You have to accept cookies before you can interact with the page.
  4. removed unnecessary sleep.

This is the result, code tested:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

options = Options()
options.add_argument("start-maximized")

# add user_agent
user_agent = "user-agent=[Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36]"
options.add_argument(user_agent)

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)  # to use over all systems
browser_delay = 2  # set if based on your connection and device speed

Content =["listtext1","listtext2","listtext3","listtext4"]
for i in range(len(Content)):
    url = "https://quillbot.com/"
    driver.get(url)
    try:
        cookie_btn = WebDriverWait(driver, browser_delay).until(EC.element_to_be_clickable((By.ID, 'onetrust-accept-btn-handler')))
        cookie_btn.click()
    except TimeoutException:
        pass  # it's a timeout or element just clicked

    Text_block = driver.find_element(By.ID, "inputText")
    Text_block.send_keys(Content[i])  # Change (fetch from Search_list)
  • Related