Home > Mobile >  How to fix not found element on Selenium
How to fix not found element on Selenium

Time:01-24

I want to run some query on here

from time import sleep
    
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
    
driver.get("https://www.scribbr.com/paraphrasing-tool/")
    
# WebDriverWait(driver, 20).until(
                # EC.presence_of_element_located((By.ID, 'paraphraser-input-box')))
    # I added the code above but still get TimeOut Exception
    
input_text_area = driver.find_element(
                By.ID, 'paraphraser-input-box')
paraphrase_button = driver.find_element(
            By.CLASS_NAME, 'MuiButton-root MuiButton-contained MuiButton-containedPrimary MuiButton-sizeMedium MuiButton-containedSizeMedium MuiButtonBase-root quillArticleBtn css-1r3yqip')

input_text_area.send_keys("multi-task")
paraphrase_button.send_keys(Keys.RETURN)

sleep(20)

output_text_area = driver.find_element(
                By.ID, 'paraphraser-output-box')

text = output_text_area.text

print(text)

I got an error of Element not found

CodePudding user response:

You have to add some wait time to handle the iframe, because without wait time it's not working all the time, try the below code:

heading = driver.find_element(By.CSS_SELECTOR, ".d-inline-block h1")
driver.execute_script("arguments[0].scrollIntoView(true)", heading)
time.sleep(1)
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "ifrm")))
time.sleep(1)
driver.find_element(By.ID, "paraphraser-input-box").send_keys("multi-task")
time.sleep(1)
driver.find_element(By.CSS_SELECTOR, ".MuiButton-root").click()

CodePudding user response:

The desired element is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following locator strategies:

    driver.get("https://www.scribbr.com/paraphrasing-tool/")
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#ifrm")))
    driver.execute_script("arguments[0].textContent = arguments[1]", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#paraphraser-input-box"))), "multi-task")
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Related