首页 > 解决方案 > 带有 Selenium 的 Python 谷歌翻译错误

问题描述

我不知道问题出在哪里,我使用了这些资源 https://medium.com/python/python-selenium-mod%C3%BCl%C3%BC-kullan%C4%B1m%C4%B1-ders-1-36983185164c https://www.youtube.com/watch?v=olOswIyeRlY 代码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

import time
driver_path = r"C:\Users\ruzgaricatsirketi\Desktop\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(executable_path=driver_path)
driver.get("https://translate.google.com/#tr/tr")
dinle = driver.find_element_by_id('gt-speech')
dinle.click()
time.sleep(2)
dinle.click()
time.sleep(3)
getir = driver.find_element_by_id('result_box').text

print(getir)

错误

DevTools listening on ws://127.0.0.1:12239/devtools/browser/76eb55d7-8aef-4b19-9d6e-fda8f4f1aee4
Traceback (most recent call last):
  File "C:\Users\ruzgaricatsirketi\Desktop\test.py", line 13, in <module>
    getir = driver.find_element_by_id('result_box').text
  File "C:\Users\ruzgaricatsirketi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "C:\Users\ruzgaricatsirketi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\ruzgaricatsirketi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\ruzgaricatsirketi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"result_box"}
  (Session info: chrome=78.0.3904.97)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.18362 x86_64)

标签: pythonselenium

解决方案


为了正确使用 Python,学习阅读堆栈跟踪至关重要。在您的情况下,这些行

File "C:\Users\ruzgaricatsirketi\Desktop\test.py", line 13, in <module>
   getir = driver.find_element_by_id('result_box').text

假设异常起源于文件test.py的第 13 行,您正在尝试搜索id属性设置为 value的 web 元素"result_box"。您遇到的异常是NoSuchElementException

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"result_box"}

正如您可以在官方文档或许多其他地方(我敢打赌有数千个)中找到的那样,当找不到您要搜索的元素时会引发此异常。

自己试试——打开你测试过的谷歌翻译页面并尝试搜索任何现有id的元素。例如,在您的测试文件的第 8 行,您尝试使用id "gt-speech"定位元素。写到document.getElementById("gt-speech")您的浏览器控制台和哇!它有效,此节点与您的搜索匹配:

<div id="gt-speech" class="speech-button goog-toolbar-button"...>...</div>

特别注意id="gt-speech"属性,它是 selenium 用来识别对象的一个​​。但是,如果您尝试使用id "result_box"进行相同操作,您将一无所获(null如果使用 JS 控制台搜索元素,则返回),因为页面上没有这样的元素(因此NoSuchElementException ...)。

我给你的建议:

  • 检查您要查找的元素(在 Windows/Linux 系统上大多数流行的 Web 浏览器上,只需右键单击元素并选择检查选项即可)

  • 选择一些独特的属性,您还可以使用id 以外的其他属性(最好不是内部文本;ID 是最好的,但它们并不总是存在...)

  • 使用这些属性使用 selenium 查找元素

并且请下次在你问之前尝试自己搜索一下,因为这是一个非常非常微不足道的问题,你应该能够在任何硒课程的前 5 分钟后处理(换句话说,UTFG:P)

编辑

PS 我可以看到您从您标记为来源的视频中获得了无效的ID 。具有这种id的元素一年前存在这一事实并不意味着它将永远存在(适用于每个平台)。如果您需要获取某个元素,则必须找到另一种方法来使用我之前提到的 selenium 定位器来定义它。


推荐阅读