gpt4 book ai didi

javascript - 属性错误 : 'WebDriver' object has no attribute 'manage' 的问题

转载 作者:行者123 更新时间:2023-11-30 14:04:08 29 4
gpt4 key购买 nike

我的代码:

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 (most recent call last): File "C:\Users\weqwwg\Desktop\python\Game.py", line 77, in driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); AttributeError: 'WebDriver' object has no attribute 'manage'

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

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 (most recent call last): File "C:\Users\Desktop\python\Game.py", line 74, in commentr.click().send_keys("HELO") AttributeError: 'NoneType' object has no attribute 'send_keys'

最佳答案

这是对原始问题的回答:

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

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"))
)

问题更新部分的更新:

I'm trying to send a key to the comment box on youtube. I removed some code, I am currently running this code.

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

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

  • 我跳过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()

关于javascript - 属性错误 : 'WebDriver' object has no attribute 'manage' 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55778961/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com