Python selenium.common.exceptions.ElementNotVisibleException:消息:元素当前不可见,因此可能无法与之交互
目前正在使用Python登录Twitter Twitter的登录页面是。用户名和密码输入字段所在的源代码:Python selenium.common.exceptions.ElementNotVisibleException:消息:元素当前不可见,因此可能无法与之交互,python,selenium,Python,Selenium,目前正在使用Python登录Twitter Twitter的登录页面是。用户名和密码输入字段所在的源代码: <div class="LoginForm-input LoginForm-username"> <input type="text" class="text-input email-input js-signin-email" name="session[username_or_email]" autocomplete="username" plac
<div class="LoginForm-input LoginForm-username">
<input
type="text"
class="text-input email-input js-signin-email"
name="session[username_or_email]"
autocomplete="username"
placeholder="Phone, email or username"
/>
</div>
<div class="LoginForm-input LoginForm-password">
<input type="password" class="text-input" name="session[password]"
placeholder="Password" autocomplete="current-password">
</div>
返回的错误为:
selenium.common.exceptions.ElementNotVisibleException:消息:元素
当前不可见,因此可能不可见
相互作用
有什么帮助吗?谢谢我读过其他类似问题的回答,但没有多大帮助
编辑:
完整错误消息:
Traceback (most recent call last):
File "main.py", line 154, in <module>
main()
File "main.py", line 143, in main
twitterBruteforce(username, wordlist, delay)
File "src/twitterLib.py", line 27, in twitterBruteforce
elem.clear()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 88, in clear
self._execute(Command.CLEAR_ELEMENT)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 457, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 233, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
at fxdriver.preconditions.visible (file:///tmp/tmpurkUhr/extensions/fxdriver@googlecode.com/components/command-processor.js:10092)
at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmpurkUhr/extensions/fxdriver@googlecode.com/components/command-processor.js:12644)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpurkUhr/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpurkUhr/extensions/fxdriver@googlecode.com/components/command-processor.js:12666)
at DelayedCommand.prototype.execute/< (file:///tmp/tmpurkUhr/extensions/fxdriver@googlecode.com/components/command-processor.js:12608)
回溯(最近一次呼叫最后一次):
文件“main.py”,第154行,在
main()
文件“main.py”,第143行,在main中
twitterBruteforce(用户名、字表、延迟)
twitterBruteforce中第27行的文件“src/twitterLib.py”
元素清除()
文件“/usr/local/lib/python2.7/dist packages/selenium/webdriver/remote/webelement.py”,第88行,清晰显示
自执行(命令清除元素)
文件“/usr/local/lib/python2.7/dist packages/selenium/webdriver/remote/webelement.py”,第457行,在
返回self.\u parent.execute(命令,参数)
文件“/usr/local/lib/python2.7/dist packages/selenium/webdriver/remote/webdriver.py”,执行中第233行
self.error\u handler.check\u响应(响应)
文件“/usr/local/lib/python2.7/dist packages/selenium/webdriver/remote/errorhandler.py”,第194行,在check_响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.ElementNotVisibleException:消息:元素当前不可见,因此可能无法与之交互
堆栈跟踪:
在fxdriver.premissions.visible(file:///tmp/tmpurkUhr/extensions/fxdriver@googlecode.com/components/command processor.js:10092)
在DelayedCommand.prototype.CheckPremissions\u(file:///tmp/tmpurkUhr/extensions/fxdriver@googlecode.com/components/command processor.js:12644)
在DelayedCommand.prototype.executeInternal\uh处(file:///tmp/tmpurkUhr/extensions/fxdriver@googlecode.com/components/command processor.js:12661)
在DelayedCommand.prototype.executeInternal\u(file:///tmp/tmpurkUhr/extensions/fxdriver@googlecode.com/components/command processor.js:12666)
在DelayedCommand.prototype.execute/<(file:///tmp/tmpurkUhr/extensions/fxdriver@googlecode.com/components/command processor.js:12608)
您应该尝试使用WebDriverWait
在与元素交互之前等待元素可见,如下所示:-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
user = wait.until(EC.visibility_of_element_located((By.NAME, "session[username_or_email]")))
user.clear()
user.send_keys(username)
pass = wait.until(EC.visibility_of_element_located((By.NAME, "session[password]")))
pass.clear()
pass.send_keys(password)
注意:-代替
发送密钥(key.RETURN)
尝试使用click()
将登录按钮元素作为login\u按钮元素。单击()
或尝试使用submit()
将表单元素作为form\u元素。submit()
找到这些元素后。您应该尝试使用WebDriverWait
等待元素可见,然后再与元素交互,如下所示:-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
user = wait.until(EC.visibility_of_element_located((By.NAME, "session[username_or_email]")))
user.clear()
user.send_keys(username)
pass = wait.until(EC.visibility_of_element_located((By.NAME, "session[password]")))
pass.clear()
pass.send_keys(password)
注意:-代替
发送密钥(key.RETURN)
尝试使用单击()
作为登录按钮元素来登录按钮元素。单击()
或尝试使用提交()
作为表单元素来提交表单元素。找到这些元素后提交()
。哪个确切的元素不可见?显示完整异常日志更新了我的帖子。谢谢:)不清楚。。你能告诉我们这个异常发生在哪一行吗??此异常是否发生在elem.clear()
行??但是您对这两个元素使用了相同的变量why???'elem=driver.find_element_by_name(“session[username_或_email]”),但此行仅查找元素。。与元素交互时可能会发生此异常,例如使用as.clear()
或send_keys()
。哪个元素不可见?显示完整异常日志更新了我的帖子。谢谢:)不清楚。。你能告诉我们这个异常发生在哪一行吗??此异常是否发生在elem.clear()
行??但是您对这两个元素使用了相同的变量why???'elem=driver.find_element_by_name(“session[username_或_email]”),但此行仅查找元素。。此异常可能在与元素交互期间发生,例如使用as.clear()
或send_keys()
。。