AttributeError:“ WebElement”对象没有属性“ getElementById”

时间:2019-04-20 20:35:26

标签: javascript python selenium automation

我正在为YouTube设置我的自动评论程序机器人。我无法将send_keys发送到评论框。这是我当前代码的副本。

commentr.getElementById('simplebox-placeholder').innerHTML = 'your comment text here'

这是发生的错误。

Traceback (most recent call last):
File "C:\Users", line 73, in <module>
commentr.getElementById('simplebox-placeholder').innerHTML = 'your 
comment text here'
AttributeError: 'WebElement' object has no attribute 'getElementById'    

当我将代码更改为

commentr.get_element_by_id('simplebox-placeholder').innerHTML = 'your comment text here'

发生相同的错误。

Traceback (most recent call last):
File "C:\Users", line 73, in <module>
commentr.get_element_by_id('simplebox-placeholder').innerHTML = 'your 
comment text here'
AttributeError: 'WebElement' object has no attribute 'get_element_by_id'

所以我将上面的代码更改为此。

driver.execute_script("arguments[0].getElementById('simplebox- 
placeholder').innerHTML = 'your comment text here';", commentr)

我在运行时遇到此错误。

Traceback (most recent call last):
File "C:\Users", line 73, in <module>
driver.execute_script("arguments[0].getElementById('simplebox- 
placeholder').innerHTML = 'your comment text here';", commentr)
File "C:\Users\craandassdo\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\selenium\webdriver\remote\webdriver.py", line 636, in 
execute_script
'args': converted_args})['value']
File "C:\Users\craandassdo\craandassdo\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\craandassdo\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.JavascriptException: Message: javascript error: 
arguments[0].getElementById is not a function
(Session info: chrome=73.0.3683.103)
(Driver info: chromedriver=73.0.3683.68 
(47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.17763 x86_64)

* /我为“ WebDriverWait”添加了预期条件模块*

这是代码。

wait = WebDriverWait(driver, 10)
commentr = wait.until(EC.element_to_be_clickable((By.ID, 'simplebox- 
placeholder')))

这是与此代码相关的错误。

Traceback (most recent call last):
File "C:\Users\Bsasadasd\Desktop\python\Game.py", line 74, in <module>
commentr = wait.until(EC.element_to_be_clickable((By.ID, 'simplebox- 
placeholder')))
File "C:\Users\sadasddo\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

1 个答案:

答案 0 :(得分:0)

尝试以下JS代码。希望它能正常工作。

 driver.execute_script("document.getElementById('simplebox-placeholder').value = 'your comment text here';")