【问题标题】:ElementNotInteractableException using selenium in python在python中使用硒的ElementNotInteractableException
【发布时间】:2020-12-03 15:16:22
【问题描述】:
import time
from selenium.webdriver.common.keys import Keys
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
#wd = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
driver =webdriver.Chrome('chromedriver',chrome_options=chrome_options)
driver.get("https://rentry.co/wftw8/edit")
driver.find_element_by_tag_name("textarea").send_keys("hello")
time.sleep(3)  
driver.find_element_by_id("id_edit_code").send_keys("iRfiNq6M")
time.sleep(3)
driver.find_element_by_id("submitButton").send_keys(Keys.ENTER)
driver.findElement(By.linkText("Save")).click()
driver.close()

在 google colab 中运行上述代码时出现此错误。

ElementNotInteractableException:消息:元素不可交互 (会话信息:headless chrome=87.0.4280.66)

任何帮助将不胜感激!

【问题讨论】:

    标签: python selenium selenium-webdriver


    【解决方案1】:

    之后进入编辑屏幕。这是一种更稳定的方法,而不是使用 time.sleep。您也可以通过这种方式开始处理异常。

    driver.get("https://rentry.co/wftw8/edit")
    wait=WebDriverWait(driver, 10)
    try:
        #wait.until(EC.element_to_be_clickable((By.TAG_NAME, "textarea"))).send_keys("hello")
        wait.until(EC.element_to_be_clickable((By.ID, "id_edit_code"))).send_keys("iRfiNq6M")
        wait.until(EC.element_to_be_clickable((By.ID, "submitButton"))).send_keys(Keys.ENTER)
        wait.until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Edit']"))).click()
        wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Save']"))).click()
        print('done')
    except:
        print('error')
    

    导入

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

    【讨论】:

      【解决方案2】:

      所以我编辑了答案,因为它是错误的,所以我再挖掘一下 发现标签textarea 并不是真正改变textarea 的值,而是跨度元素内的innerText。要更改该 innerText 您需要选择 span 并调用 execute_script,让我们更好地向您展示如何操作:

      import time
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      from selenium import webdriver
      from selenium.webdriver.common.keys import Keys
      
      chrome_options = webdriver.ChromeOptions()
      # note I commented this to see 
      # chrome_options.add_argument('--headless')
      # chrome_options.add_argument('--no-sandbox')
      # chrome_options.add_argument('--disable-dev-shm-usage')
      driver =webdriver.Chrome(executable_path='chromedriver',chrome_options=chrome_options)
      
      driver.get("https://rentry.co/wftw8/edit")
      try:
      
          # here I selected the **span** element that I talk above
          span = driver.find_element_by_xpath('//*[@id="text"]/div/div[5]/div[1]/div/div/div/div[5]/pre/span')
          # change the innerText thwough js
          driver.execute_script('arguments[0].innnerText="Hello Lola"', span)
          # just wait for the id_edit_code to be present
          edit = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "id_edit_code")))
          edit.send_keys("hjhkjhjh")
          # and same as you had it
          driver.find_element_by_id("submitButton").send_keys(Keys.ENTER)
      
      finally:
          driver.close()
      
      

      这个question 很有帮助。

      【讨论】:

      • 尝试散列行 driver.findElement(By.linkText("Save")).click() ,然后错误也是一样的
      • 你能试着评论一下time.sleep(3) driver.find_element_by_id("id_edit_code").send_keys("iRfiNq6M") time.sleep(3) driver.find_element_by_id("submitButton").send_keys(Keys.ENTER) driver.findElement(By.linkText("Save")).click() driver.close() 并告诉我发生了什么吗??
      • 因为也许您需要告诉 selenium 等待元素出现,例如 this
      • 所以问题似乎是你没有等待元素出现。看看我在上面传递给你的链接。我没有 chrome 驱动程序,所以我无法测试您的代码:(
      • 有哪些变化?
      猜你喜欢
      • 2022-01-16
      • 2018-02-07
      • 2021-04-27
      • 1970-01-01
      • 2018-11-23
      • 1970-01-01
      • 2021-01-08
      • 2020-11-05
      • 1970-01-01
      相关资源
      最近更新 更多