【问题标题】:Error message - Element not interactable Selenium webdriver [duplicate]错误消息 - 元素不可交互 Selenium webdriver [重复]
【发布时间】:2019-08-29 12:13:02
【问题描述】:

我创建了以下代码,用于从 webiste 中提取数据并转换为 excel。

我将数据导入 excel 没有问题,但是有许多手风琴开关隐藏了一些我试图打开的数据。

但是我收到“元素不可交互”错误。我已经看到了许多与此错误类似的问题,但我无法确定为什么这不起作用?

(手风琴开关工作正常 - 但它说不可见?)

澳大利亚网站顺便说一句。

请参阅下面的完整代码和错误:

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


chrome_path =r"C:\Users\Tom\Desktop\chromedriver.exe"

d = webdriver.Chrome(chrome_path)
d.get("https://pointsbet.com.au/basketball/NCAA-March-Madness")

time.sleep(2)

d.find_element_by_xpath("""/html/body/div[1]/div[2]/sport-competition-component/div[1]/div[2]/div[1]/div/event-list/div[1]/event/div/header/div[1]/h2/a""").click()
time.sleep(2)

expandable = WebDriverWait(d, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".h2.accordion-toggle.event-name")))
expandables = d.find_elements_by_css_selector('.h2.accordion-toggle.event-name')
for item in expandables:
    item.click()


posts = d.find_elements_by_class_name("market")
for post in posts:
    print(post.text)
    with open('output.xls',mode ='a') as f:
        f.write(post.text)
        f.write('\n')

d.quit()

错误:

Traceback (most recent call last):
  File "C:\Users\Tom\Desktop\Python test\points1 - Copy.py", line 21, in <module>
    item.click()
  File "C:\Users\Tom\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\Tom\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\Tom\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Tom\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
  (Session info: chrome=73.0.3683.86)
  (Driver info: chromedriver=2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a),platform=Windows NT 10.0.17134 x86_64)

任何帮助将不胜感激。

【问题讨论】:

  • 你在哪个元素上得到了这个异常
  • 完整错误现在在原始帖子中(点击元素)
  • 你告诉我们你在哪一行,我们怎么知道??
  • 对不起,我是编码新手。我很确定它的第 21 行
  • 你能分享一下你想用开发工具找到哪个元素的网页屏幕截图吗

标签: python selenium selenium-webdriver element


【解决方案1】:

使用Action 类点击元素。

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

d.get("https://pointsbet.com.au/basketball/NCAA-March-Madness")

WebDriverWait(d,10).until(EC.element_to_be_clickable((By.XPATH,'//h2/a[@class="ng-binding"]'))).click()


expandable = WebDriverWait(d, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".h2.accordion-toggle.event-name")))
expandables = d.find_elements_by_css_selector('.h2.accordion-toggle.event-name')
for item in expandables:
  ActionChains(d).move_to_element(item).click().perform() # item.click()



posts = d.find_elements_by_class_name("market")
for post in posts:
    print(post.text)


d.quit()

【讨论】:

  • 感谢 Kajal,效果很好。作为一个附带问题,有没有办法在循环中重复代码并在 WebDriverWait(d,10).until(EC.element_to_be_clickable((By.XPATH,'//h2/a[@class= "ng-binding"]'))).click() As 这将帮助我在任何时候收集多个匹配项的数据。
  • 我要检查一下那个伙伴。目前不在工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 2019-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多