首页 > 解决方案 > 用于 Youtube 的 Python Selenium Opera - 出现错误

问题描述

我正在尝试来自以下 URL 的代码,它似乎适用于 带有 selenium python 的 Opera-Drive Opera

import time

from selenium import webdriver
from selenium.webdriver.chrome import service


webdriver_service = service.Service('.\operadriver.exe')
webdriver_service.start()

driver = webdriver.Remote(webdriver_service.service_url, webdriver.DesiredCapabilities.OPERA)

driver.get('https://www.google.com/')
input_txt = driver.find_element_by_name('q')
input_txt.send_keys('operadriver\n')

time.sleep(5) #see the result
driver.quit()

但是尝试使用以下 Xpath 代码在 Youtube 中搜索时出现错误-

driver.get('https://youtube.com/')
input_txt = driver.find_element_by_xpath('//*[@id="search"]')
input_txt.send_keys('operadriver\n')

错误

Traceback (most recent call last):
  File "C:\Users\admin\Desktop\py\-test\web\selnm\ytb1.py", line 39, in <module>
    input_txt.send_keys('operadriver\n')
  File "C:\Users\admin\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webelement.py", line 479, in send_keys
    'value': keys_to_typing(value)})
  File "C:\Users\admin\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\admin\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\admin\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.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=93.0.4577.82)
  (Driver info: operadriver=93.0.4577.63 (ff5c0da2ec0adeaed5550e6c7e98417dac77d98a-refs/branch-heads/4577@{#1135}),platform=Windows NT 6.1.7601 SP1 x86_64)

有什么建议为什么它不适用于 youtube?

标签: pythonpython-3.xseleniumopera

解决方案


尝试这个

driver.get('https://youtube.com/')
input_txt = driver.find_element_by_xpath('//input[@id="search"]')
input_txt.click()
time.sleep(1)
input_txt.send_keys('operadriver\n')

推荐阅读