【问题标题】:Issue with AttributeError: 'WebDriver' object has no attribute 'manage'AttributeError 问题:“WebDriver”对象没有“管理”属性
【发布时间】:2019-09-10 17:38:29
【问题描述】:

我的代码:

commentr = driver.find_element_by_id("simplebox-placeholder")
commentr.click()

driver.execute_script("document.getElementById('simplebox- 
placeholder').value = 'your comment text here';")
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
commentr.send_keys("HELO")

我的错误:

Traceback(最近一次调用最后一次):文件 “C:\Users\weqwwg\Desktop\python\Game.py”,第 77 行,在 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); AttributeError: 'WebDriver' 对象没有属性 'manage'

我正在尝试向 youtube 上的评论框发送密钥。我删除了一些代码,我目前正在运行此代码。

commentr = driver.find_element_by_id("simplebox-placeholder")
commentr.click()
driver.implicitly_wait(10)
commentr.send_keys("HELO")

这是我得到的错误:

Traceback (most recent call last):
  File "C:\Users\Brandsdo\Desktop\python\Game.py", line 76, in <module>
    commentr.send_keys("HELO")
  File "C:\Users\Braasdasndo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 479, in send_keys
    'value': keys_to_typing(value)})
  File "C:\Users\Brsadasdando\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\Braasdasndo\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\Braasdando\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.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=73.0.3683.103)
  (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.17763 x86_64)

更新的部分代码

driver.find_element_by_id("simplebox-placeholder").click()

commentr = WebDriverWait(driver,10).until(EC.element_to_be_clickable( (By.ID, 'contenteditable-textarea') ))

commentr.click().send_keys("HELO")
driver.find_element_by_id("submit-button").click()

这是错误

Traceback(最近一次调用最后一次): 文件“C:\Users\Desktop\python\Game.py”,第 74 行,在 commentr.click().send_keys("HELO") AttributeError: 'NoneType' 对象没有属性 'send_keys'

【问题讨论】:

  • 如果设置了这个超时,你的想法是什么?您能否在问题中直接解释。我觉得,你使用了错误的方法。要解决您眼前的问题,请使用driver.implicitly_wait(10)。见there
  • 问题是您的driver.manage... 行是Java,但您是用python 编写的。您需要使用 Sergey 指示的 python 等价物。

标签: javascript python selenium selenium-webdriver


【解决方案1】:

这是对原始问题的回答:

要解决您眼前的问题,请使用

driver.implicitly_wait(10)

手册是there

但是,您可能完全走错了方向。 相反,请尝试使用WebDriverWait 模块。

from selenium.webdriver.support.ui import WebDriverWait

例如:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#...

footer = WebDriverWait(driver, 10).until(EC.visibility_of_element_located(
   (By.CSS_SELECTOR, ".b-footer__divider"))
)

问题更新部分的更新:

我正在尝试向 youtube 上的评论框发送密钥。我删除了一些代码,我目前正在运行此代码。

正如我所怀疑的,您根本不需要 implicitly_wait 函数。

  • 我已经查看了 YouTube 页面。您的第一步是正确的 - 您正在找到“添加公共评论...”框并单击它。

  • 我跳过了 implicitly_wait 调用 - 它不会影响那里的任何内容。

  • 在下一步中,您尝试将击键发送到您找到并单击的同一个框。这是错误的。尽管它们看起来完全一样,但您单击的是 ID 为 simplebox-placeholder 的元素,但一旦单击该元素就会变得不可见,而 ID 为 contenteditable-textarea 的相同外观元素已准备好获取您的输入。

    李>

在一个简单的方法中,您应该找到这个元素并将击键发送到它:

commentr = driver.find_element_by_id("contenteditable-textarea")
commentr.click()
commentr.send_keys("HELO")

但是当您点击simplebox-placeholder 时,页面可能需要一些时间来执行必要的操作并使contenteditable-textarea 可见且可点击。如果元素尚未准备好,以下方法将允许您避免异常:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

commentr = WebDriverWait(driver,10).until(EC.element_to_be_clickable( (By.ID, 'contenteditable-textarea') ))
commentr.click()
commentr.send_keys("HELO")
  • 最后,找到“评论”按钮并单击它提交您的评论。在这里您可以使用简化的方法,因为“评论”按钮已经准备好:

driver.find_element_by_id("submit-button").click()

总体而言,您的代码可能如下所示:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver.find_element_by_id("simplebox-placeholder").click()

commentr = WebDriverWait(driver,10).until(EC.element_to_be_clickable( (By.ID, 'contenteditable-textarea') ))

commentr.click()
commentr.send_keys("HELO")
driver.find_element_by_id("submit-button").click()

【讨论】:

  • 我仍然有错误谢尔盖。我将更新帖子以向您展示发生了什么。
  • 我不明白。
  • @justloookingforlove,你应该在更新帖子时给我发评论。现在很清楚您要完成什么以及有什么问题。让我更新我的答案
  • 你能解释一下吗?我真的很想知道我的错误。
  • 成功了。出色的谢尔盖。非常感谢您花时间和精力帮助我!
猜你喜欢
  • 2021-06-23
  • 1970-01-01
  • 2022-06-29
  • 2022-06-25
  • 2022-11-19
  • 2023-01-07
  • 2016-10-14
  • 2021-05-24
  • 2022-09-29
相关资源
最近更新 更多