简体   繁体   中英

Element Not interactable Selenium Python

I am trying to send keys to the text box on https://www.wolframalpha.com/ but as you can see I was hit with an error. I've tried looking thru the elements but nothing seems to be working if you are able to help I will be thankful. using python3.7, my code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time 
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions() 
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(options=options, executable_path=r'C:\Program Files (x86)\Chrome\Application\chromedriver.exe')

driver.get('https://www.wolframalpha.com/')

driver.find_element_by_xpath('_9CcbX').send_keys('1+1')

error

traceback (most recent call last):
  File "c:/Users/gabri/Desktop/BOT/IXL Bot - Gabriel.py", line 15, in <module>
    driver.find_element_by_xpath('_9CcbX').send_keys('1+1')
  File "C:\Users\gabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Users\gabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\gabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\gabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"_9CcbX"}        
  (Session info: chrome=88.0.4324.146)

First of all, you get NoSuchElementException , because you are searching by xpath, with class-name argument. ( _9CcbX is a class name). Instead, you can copy element's correct xpath:

  • inspect
  • right-click element
  • copy -> copy full xpath

This will give you:

driver.find_element_by_xpath('/html/body/div/div/div/div/div/div[1]/section/form/div/div/input').send_keys('1+1')

Second of all, you should use WebdriverWait to be sure the element has loaded and is interactable.

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

xpath = '/html/body/div/div/div/div/div/div[1]/section/form/div/div/input'
element = wait.until(EC.element_to_be_clickable(driver.find_element(By.XPATH, xpath)))
element.send_keys('1+1')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM