首页 > 解决方案 > selenium wedriver程序无法继续

问题描述

我正在尝试使用 webdriver 单击登录按钮并且页面已正确转换。但是程序停止并发生了问题“selenium.common.exceptions.ElementNotInteractableException:消息:元素不可交互”,这是发生问题的代码

browser.find_element_by_xpath(
            '//*[@id="emap-rsids-content"]/div/div[3]/div/div[1]/div/div/div/input').send_keys(uid)
        browser.find_element_by_xpath(
            '//*[@id="emap-rsids-content"]/div/div[3]/div/div[2]/div/div/div/input').send_keys(pwd)
        # click to sign in

        browser.find_element_by_xpath('//*[@id="emap-rsids-content"]/div/div[3]/div/div[3]/div/button').send_keys(Keys.ENTER)
        time.sleep(3)
        browser.find_element_by_xpath('/html/body/main/article/section[1]/div/div/div/div[2]/div/div/div[2]/div[2]').click()
这是回溯

Traceback (most recent call last):
  File "C:/Users/14638/Desktop/auto_sign_zzu_jksb-master/auto_sign.py", line 68, in sign_in
    time.sleep(3)
  File "C:\Users\14638\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\14638\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\14638\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\14638\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=83.0.4103.116)
第 68 行是 time.sleep(3),点击有效并转换到新页面,但仍然出现按钮元素是可交互的。我尝试了两种方法,一种是

''' browser.find_element_by_xpath('//*[@id="emap-rsids-content"]/div/div[3]/div/div[3]/div/button').click() '''

另一个是

''' pages=browser.find_element_by_xpath('//*[@id="emap-rsids-content"]/div/div[3]/div/div[3]/div/button') browser.execute_script(" arguments[0].click();", pages) '''

但还是不行

标签: pythonseleniumselenium-webdriver

解决方案


我认为您正在尝试单击未完全加载的元素。您要做的就是等到发生这种情况。

首先导入这些文件

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

然后登录后添加。

WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/main/article/section[1]/div/div/div/div[2]/div/div/div[2]/div[2]')))

然后做

browser.find_element_by_xpath('/html/body/main/article/section[1]/div/div/div/div[2]/div/div/div[2]/div[2]').click()

推荐阅读