消息:元素不可交互发送密钥时出现错误[search_bar.send_keys(course_name)]使用硒python搜索YouTube栏

时间:2020-04-29 08:20:17

标签: python selenium selenium-webdriver

我尝试了StackOverflow的大多数解决方案,但对我不起作用 我正在尝试使用硒python将一些课程名称发送到youtube搜索栏,但之前效果很好,但现在在执行此操作时出现此错误 而且search_bar.send_keys(course_name)适用于其他网站,但不适用于YouTube

Traceback (most recent call last):
      File "src/gevent/greenlet.py", line 766, in gevent._greenlet.Greenlet.run
      File "/home/sh4d0w/PycharmProjects/AutoMate/venv/lib/python3.7/site-packages/eel/__init__.py", line 257, in _process_message
        return_val = _exposed_functions[message['name']](*message['args'])
      File "/home/sh4d0w/PycharmProjects/AutoMate/SmallTalk.py", line 72, in SingleQueryinputValue
        RecommendCourse.getUdacityCourse(str(val))
      File "/home/sh4d0w/PycharmProjects/AutoMate/RecommendCourse.py", line 160, in getUdacityCourse
        getYoutubeCourse(course_name, driver)
      File "/home/sh4d0w/PycharmProjects/AutoMate/RecommendCourse.py", line 98, in getYoutubeCourse
        search_bar.send_keys(course_name)
      File "/home/sh4d0w/PycharmProjects/AutoMate/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 479, in send_keys
        'value': keys_to_typing(value)})
      File "/home/sh4d0w/PycharmProjects/AutoMate/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
        return self._parent.execute(command, params)
      File "/home/sh4d0w/PycharmProjects/AutoMate/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
        self.error_handler.check_response(response)
      File "/home/sh4d0w/PycharmProjects/AutoMate/venv/lib/python3.7/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=81.0.4044.129)

    2020-04-29T08:00:02Z <Greenlet at 0x7fd2089c67b8: _process_message({'call': 2.1877049007713376, 'name': 'SingleQueryi, <geventwebsocket.websocket.WebSocket object at 0x7)> failed with ElementNotInteractableException

代码示例

option = webdriver.ChromeOptions()
option.add_argument("window-size=1200x600");
driver = webdriver.Chrome('/usr/bin/chromedriver', options=option)
driver.get("https://www.youtube.com")
getYoutubeCourse(course_name, driver)

getYoutubeCourse()函数主体

def getYoutubeCourse(course_name, driver):
    time.sleep(2)

    search_bar = driver.find_element_by_xpath('//*[@id="search"]')
    search_bar.send_keys(course_name)

    search_bar_button = WebDriverWait(driver, 5).until(EC.element_to_be_clickable(
        (By.XPATH, '//*[@id="search-icon-legacy"]')))
    search_bar_button.click()
    ......

然后,在此之后,抓取youtube链接的逻辑就存在了 我还尝试了所有的Web驱动程序等待,并且我的驱动程序也是最新的

请帮助我是python和硒新手

4 个答案:

答案 0 :(得分:2)

xpath找到3个元素://*[@id="search"]

您必须将其更正为

//input[@id="search"]

答案 1 :(得分:1)

我也遇到了同样的问题。经过数小时的痛苦,我终于找到了解决方案。 复制xpath(或您用来引用它的任何其他ID)时,它指向整个搜索框容器。经过反复的试验和观察,我发现(其中包含您可以在开发人员中查看的网页中的)特定元素是需要查找的输入:

<input id="search" autocapitalize="none" autocomplete="off" autocorrect="off" name="search_query" tabindex="0" type="text" spellcheck="false" placeholder="Search" aria-label="Search" aria-haspopup="false" role="combobox" aria-autocomplete="list" dir="ltr" class="ytd-searchbox" style="outline: none;">

复制xpath时的问题是您获得了容器的ID。

因此请使用“复制完整的xpath”。这将引用容器中可以输入的特定元素。

搜索框输入元素的完整xpath是:

'/html/body/ytd-app/div/div/ytd-masthead/div[3]/div[2]/ytd-searchbox/form/div/div[1]/input'

您可以像这样使用它:

search_bar = driver.find_element_by_xpath('/html/body/ytd-app/div/div/ytd-masthead/div[3]/div[2]/ytd-searchbox/form/div/div[1]/input')
search_bar.send_keys(song_name)
time.sleep(1)#You can skip this.
search_bar.send_keys(Keys.ENTER)

P.S。请原谅术语。我也是初学者,这是我的第一个答案。

答案 2 :(得分:0)

您可以尝试以下方法:

from selenium.webdriver.common.keys import Keys
def getYoutubeCourse(course_name, driver):
    time.sleep(2)

    search_bar = driver.find_element_by_xpath('//input[@id="search"]')
    search_bar.send_keys(course_name)

    time.sleep(1)
    search_bar.send_keys(Keys.ENTER)
    ....

答案 3 :(得分:0)

我也是遇到同样问题的人。 我尝试使用完整的Xpath,但仍无法正常工作。然后,我在xpath之后加了[0],它起作用了。

搜索框输入元素的完整xpath是:

'/html/body/ytd-app/div/div/ytd-masthead/div[3]/div[2]/ytd-searchbox/form/div/div[1]/input[0]'

这是完整的源代码

chrome_driver = '/Users/Downloads/chromedriver'

driver = webdriver.Chrome(chrome_driver)

driver.get('https://www.youtube.com')

search_bar = driver.find_elements_by_xpath('/html/body/ytd-app/div/div/ytd-masthead/div[3]/div[2]/ytd-searchbox/form/div/div[1]/input')[0]

time.sleep(2)
search_bar.send_keys("black pink")

time.sleep(2)
search_bar.send_keys(Keys.RETURN)

谢谢