AttributeError 问题:“WebDriver”对象没有属性“manage”

jus*_*ove 2 javascript python selenium selenium-webdriver

我的代码:

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")
Run Code Online (Sandbox Code Playgroud)

我的错误:

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

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

commentr = driver.find_element_by_id("simplebox-placeholder")
commentr.click()
driver.implicitly_wait(10)
commentr.send_keys("HELO")
Run Code Online (Sandbox Code Playgroud)

这是我收到的错误:

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)
Run Code Online (Sandbox Code Playgroud)

更新了部分代码

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()
Run Code Online (Sandbox Code Playgroud)

这就是错误

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

Ser*_*nov 6

这是对一个原始问题的回答:

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

driver.implicitly_wait(10)
Run Code Online (Sandbox Code Playgroud)

说明书

然而,你可能完全走错了方向。相反,尝试使用该WebDriverWait模块。

from selenium.webdriver.support.ui import WebDriverWait
Run Code Online (Sandbox Code Playgroud)

例如:

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"))
)
Run Code Online (Sandbox Code Playgroud)

问题更新部分的更新:

我正在尝试向 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")
Run Code Online (Sandbox Code Playgroud)

但是,当您单击 时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")
Run Code Online (Sandbox Code Playgroud)
  • 最后,找到“评论”按钮并单击它来提交您的评论。这里你可以使用简化的方法,因为“评论”按钮已经准备好了:

driver.find_element_by_id("submit-button").click()
Run Code Online (Sandbox Code Playgroud)

总的来说,您的代码可能如下所示:

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()
Run Code Online (Sandbox Code Playgroud)

  • 有效。才华横溢的谢尔盖。非常感谢您花时间和精力帮助我! (2认同)