Python 3.x Webscraping:ElementNotInteractiableException在尝试填写此网站上的表单时发生

Python 3.x Webscraping:ElementNotInteractiableException在尝试填写此网站上的表单时发生,python-3.x,selenium,selenium-webdriver,xpath,webdriverwait,Python 3.x,Selenium,Selenium Webdriver,Xpath,Webdriverwait,我一直在试图从中获取数据。我需要填写获得您汽车的准确价格表格,如年份、品牌、型号等。。到目前为止,我已经编写了以下代码: import requests import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriv

我一直在试图从中获取数据。我需要填写获得您汽车的准确价格表格,如年份、品牌、型号等。。到目前为止,我已经编写了以下代码:

import requests
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.support import expected_conditions
from bs4 import BeautifulSoup
import re

chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome('chromedriver_win32/chromedriver.exe', options=chrome_options)
url = "http://www.indianbluebook.com/"
driver.get(url)
save_city = driver.find_element_by_xpath('//*[@id="cityPopup"]/div[2]/div/div[2]/form/div[2]/div/a[1]').click()  #Bangalore

#fill year
year_dropdown = Select(driver.find_element_by_xpath('//*[@id="car_value"]/div[2]/div[1]/div[1]/div/select'))
driver.implicitly_wait(50)
year_dropdown.select_by_value('2020')

time.sleep(5)
但是,它给出了这样一个错误:

ElementNotInteractableException           Traceback (most recent call last)
<ipython-input-25-a4eb8001e649> in <module>
      8 year_dropdown = Select(driver.find_element_by_xpath('//*[@id="car_value"]/div[2]/div[1]/div[1]/div/select'))
      9 driver.implicitly_wait(50)
---> 10 year_dropdown.select_by_value('2020')
     11 
     12 time.sleep(5)

~\anaconda3\lib\site-packages\selenium\webdriver\support\select.py in select_by_value(self, value)
     80         matched = False
     81         for opt in opts:
---> 82             self._setSelected(opt)
     83             if not self.is_multiple:
     84                 return

~\anaconda3\lib\site-packages\selenium\webdriver\support\select.py in _setSelected(self, option)
    210     def _setSelected(self, option):
    211         if not option.is_selected():
--> 212             option.click()
    213 
    214     def _unsetSelected(self, option):

~\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py in click(self)
     78     def click(self):
     79         """Clicks the element."""
---> 80         self._execute(Command.CLICK_ELEMENT)
     81 
     82     def submit(self):

~\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py in _execute(self, command, params)
    631             params = {}
    632         params['id'] = self._id
--> 633         return self._parent.execute(command, params)
    634 
    635     def find_element(self, by=By.ID, value=None):

~\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

~\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated
  (Session info: chrome=85.0.4183.102)
ElementNotInteractiableException回溯(最近一次调用)
在里面
8年下拉菜单=选择(驱动程序。通过xpath('/*[@id=“car\u value”]/div[2]/div[1]/div[1]/div/Select')查找元素)
9.隐式等待(50)
--->10年期下拉列表。按值选择(2020年)
11
12次。睡眠(5)
~\anaconda3\lib\site packages\selenium\webdriver\support\select.py按值选择(self,value)
80匹配=错误
81选择加入选项:
--->82自选(opt)
83如果不是self.is_multiple:
84返回
~\anaconda3\lib\site packages\selenium\webdriver\support\select.py in\u setSelected(self,option)
210 def_setSelected(自选,选件):
211如果未选择选项。是否已选择()
-->212选项。单击()
213
214已选择def_(自身,选项):
单击中的~\anaconda3\lib\site packages\selenium\webdriver\remote\webelement.py(self)
78 def单击(自身):
79“单击元素。”“”
--->80自执行(命令。单击元素)
81
82 def提交(自我):
~\anaconda3\lib\site packages\selenium\webdriver\remote\webelement.py in\u execute(self、command、params)
631参数={}
632参数['id']=self.\u id
-->633返回self.\u parent.execute(命令,参数)
634
635 def find_元素(self,by=by.ID,value=None):
执行中的~\anaconda3\lib\site packages\selenium\webdriver\remote\webdriver.py(self,driver\u命令,参数)
319 response=self.command\u executor.execute(driver\u command,params)
320如果响应:
-->321自我错误处理程序检查响应(响应)
322响应['value']=self.\u展开值(
323响应。获取('值',无))
检查响应中的~\anaconda3\lib\site packages\selenium\webdriver\remote\errorhandler.py(self,response)
240警报文本=值['alert']。获取('text')
241引发异常类(消息、屏幕、堆栈跟踪、警报文本)
-->242引发异常类(消息、屏幕、堆栈跟踪)
243
244定义值或默认值(self、obj、key、default):
ElementNotInteractitableException:消息:元素不可交互:元素当前不可见且不能被操作
(会话信息:chrome=85.0.4183.102)

注意:我在互联网上尝试了许多可用的解决方案,比如使用WebDriverWait的预期条件。有时会出现错误,
StaleElementException
。我不知道现在该怎么办。请帮忙。我不熟悉这一点。

您可以使用下面的方法实现同样的效果

#Set link according to data need
driver.get('http://www.indianbluebook.com/')

#Wait webpage to fully load necessary tables
def ajaxwait():
    for i in range(1, 30):
        x = driver.execute_script("return (window.jQuery != null) && jQuery.active")
        time.sleep(1)
        if x == 0:
            print("All Ajax element loaded successfully")
            break


ajaxwait()
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='cityPopup']/div[2]/div/div[2]/form/div[2]/div/a[1]")))

save_city = driver.find_element_by_xpath('//*[@id="cityPopup"]/div[2]/div/div[2]/form/div[2]/div/a[1]').click()  #Bangalore

ajaxwait()
#fill year
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='form-group']//select[@class='form-control' and @name='manufacture_year']/following-sibling::div/a")))
#//div[@class='form-group']//select[@class='form-control' and @name='manufacture_year'] this is the only unique elemnt with reference to this we can find other element.
#click on select year field then a dropdown will be open we will enter the year in the input box. Then select the item from the ul list.
driver.find_element_by_xpath("//div[@class='form-group']//select[@class='form-control' and @name='manufacture_year']/following-sibling::div/a").click()
driver.find_element_by_xpath("//div[@class='form-group']//select[@class='form-control' and @name='manufacture_year']/following-sibling::div//input").send_keys("2017")
driver.find_element_by_xpath("//div[@class='form-group']//select[@class='form-control' and @name='manufacture_year']/following-sibling::div//em").click()
同样,您可以通过更改
@name='manufacture\u year'
属性值来选择其他下拉列表


注意:使用Ajax wait更新了代码。

您可以使用下面的方法来实现相同的功能

#Set link according to data need
driver.get('http://www.indianbluebook.com/')

#Wait webpage to fully load necessary tables
def ajaxwait():
    for i in range(1, 30):
        x = driver.execute_script("return (window.jQuery != null) && jQuery.active")
        time.sleep(1)
        if x == 0:
            print("All Ajax element loaded successfully")
            break


ajaxwait()
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='cityPopup']/div[2]/div/div[2]/form/div[2]/div/a[1]")))

save_city = driver.find_element_by_xpath('//*[@id="cityPopup"]/div[2]/div/div[2]/form/div[2]/div/a[1]').click()  #Bangalore

ajaxwait()
#fill year
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='form-group']//select[@class='form-control' and @name='manufacture_year']/following-sibling::div/a")))
#//div[@class='form-group']//select[@class='form-control' and @name='manufacture_year'] this is the only unique elemnt with reference to this we can find other element.
#click on select year field then a dropdown will be open we will enter the year in the input box. Then select the item from the ul list.
driver.find_element_by_xpath("//div[@class='form-group']//select[@class='form-control' and @name='manufacture_year']/following-sibling::div/a").click()
driver.find_element_by_xpath("//div[@class='form-group']//select[@class='form-control' and @name='manufacture_year']/following-sibling::div//input").send_keys("2017")
driver.find_element_by_xpath("//div[@class='form-group']//select[@class='form-control' and @name='manufacture_year']/following-sibling::div//em").click()
同样,您可以通过更改
@name='manufacture\u year'
属性值来选择其他下拉列表


注意:使用Ajax wait更新了代码。

要单击班加罗尔,然后从下拉列表中选择2020,您需要将
元素归纳为可点击()
,您可以使用以下选项:

  • 使用
    XPATH

    driver.get('http://www.indianbluebook.com/')
    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.LINK_TEXT, "BANGALORE"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Select Year']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='chosen-results']//li[@class='active-result' and text()='2020']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 浏览器快照:


要单击班加罗尔,然后从下拉列表中选择2020,您需要使
元素可点击()
,您可以使用以下选项:

  • 使用
    XPATH

    driver.get('http://www.indianbluebook.com/')
    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.LINK_TEXT, "BANGALORE"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Select Year']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='chosen-results']//li[@class='active-result' and text()='2020']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 浏览器快照:


请检查这一点,您是否可以使用其他方法?@dilimpeghwal当然可以。只要我继续进行报废,任何方法都可以。请检查这一点。你可以用其他方法吗?@dilimpeghwal当然可以。只要我继续进行报废,任何方法都可以。它会产生同样的错误。StaleElementReferenceException:Message:stale元素引用:元素未附加到页面文档(会话信息:chrome=85.0.4183.102),有时它正在工作,有时它会引发该异常。为什么以及如何避免这种情况?这些是Ajax元素,需要适当的等待。你可以参考这个。或者我们必须使用JavaScriptExecutor。我已经尝试增加等待时间。但是它经常抛出异常。添加代码以等待jQuery正确加载。我已申请最多30秒等待,请相应更新。您好。它会产生同样的错误。StaleElementReferenceException:Message:stale元素引用:元素未附加到页面文档(会话信息:chrome=85.0.4183.102),有时它正在工作,有时它会引发该异常。为什么以及如何避免这种情况?这些是Ajax元素,需要适当的等待。你可以参考这个。或者我们必须使用JavaScriptExecutor。我已经尝试增加等待时间。但是它经常抛出异常。添加代码以等待jQuery正确加载。我已申请最多30秒的等待,相应更新。