1
from selenium import webdriver
import time, random
import threading

url = 'https://npm.runkit.com/'

def e():

      driver = webdriver.Chrome()
      driver.get(url)
      time.sleep(10)
      driver.find_element_by_xpath('/html/body/div/div/div/div/div/div/div/div[3]/div/div/div/div[1]/div[1]/div[6]/div[1]/div/div/div/div[5]/div/pre/span/span').send_keys('a')


for i in range(1):
    t = threading.Thread(target=e)
    t.start()

Image:

The Image

What I'm trying to do is make the script write in this dialog box where something can be written but i can't select the path always because of this error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element

Could someone do it for me?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

It seems you had copied the wrong path

Please find the code below:

from selenium import webdriver
import time, random
import threading
import os
cwd = os.chdir('C:/Users/huzi95s/Desktop')
url = 'https://npm.runkit.com/'

def e():

     driver = webdriver.Chrome()
     driver.get(url)
     time.sleep(10)
     driver.find_element_by_xpath('//*[@id="react-container"]/div/div/div/div[1]/div/div/div[1]/div[2]/div[1]/div/div')

for i in range(1):
  t = threading.Thread(target=e)
  t.start()

This gives you the dialog box as shown:

dialog

The trick is to copy XPath and not the full Xpath. This will get rid of the error message

Huzefa Sadikot
  • 561
  • 1
  • 7
  • 22
  • this still gives the error of (selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:) I would like to make the script by typing in the dialog box – Nicolas Harleen Dec 12 '20 at 07:52
  • This does not in my browser. See my screenshot. Wait for sometime it will automatically open the browser and load the page for you. After the page is loaded it gives me the same error in my case too. – Huzefa Sadikot Dec 12 '20 at 07:52
  • the dialog box appears automatically when you wait on the site i would like to make the script recognize it and type the error is strange – Nicolas Harleen Dec 12 '20 at 07:55
  • 1
    Use explicit waits https://www.techbeamers.com/selenium-webdriver-waits-python/ – ou_ryperd Dec 12 '20 at 08:29
0

To send a character sequence within the desired field you need 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://npm.runkit.com/")
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://runkit.com/e/iframe')]")))
    time.sleep(10)
    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.CodeMirror")))
    ActionChains(driver).move_to_element(element).click().send_keys("a").perform()
    
  • 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
    from selenium.webdriver.common.action_chains import ActionChains
    
  • Browser Snapshot:

CodeMirror


References

You can find a couple of relevant discussions on NoSuchElementException in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352