python - Selenium -python 元素不可点击

标签 python facebook selenium

我正在尝试通过 python 中的 selenium 浏览 facebook。 这是到目前为止我的脚本。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

usr = ""# I have put 3 different accounts and tested it. Same error
pwd = ""

driver = webdriver.Chrome('E:\python_libs\chromedriver.exe')

driver.get("https://www.facebook.com")

assert "Facebook" in driver.title

elem = driver.find_element_by_id("email")
elem.send_keys(usr)
elem = driver.find_element_by_id("pass")
elem.send_keys(pwd)
elem.send_keys(Keys.RETURN)



elem = driver.find_element_by_css_selector(".input.textInput")
elem.send_keys("Posted using Python's Selenium WebDriver bindings!")



elem = driver.find_element_by_css_selector(".selected")

elem.click()
time.sleep(5)




driver.close()

此脚本在获取并正确安装了 chromedriver 的 Windows 上运行时会返回错误。

Traceback (most recent call last):
File "C:\Users\Home\Desktop\facebook_post.py", line 28, in <module>
elem.click()
File "C:\Python27\lib\site-packages\selenium-2.42.0-           py2.7.egg\selenium\webdriver\remote\webelement.py", line 60, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python27\lib\site-packages\selenium-2.42.0-    py2.7.egg\selenium\webdriver\remote\webelement.py", line 370, in _execute
return self._parent.execute(command, params)
File "C:\Python27\lib\site-packages\selenium-2.42.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 172, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium-2.42.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 164, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: u'unknown error: Element is not clickable at point (481,     185). Other element would receive the click: <input type="file" class="_n _5f0v" title="Choose a file to upload" accept="image/*" name="file" id="js_0">\n  (Session info: chrome=35.0.1916.114)\n  (Driver info: chromedriver=2.10.267521,platform=Windows NT 6.1     x86)'

我对此一无所知。 任何帮助将不胜感激。

Graph API 在这里不可行,因为我想以我自己的身份浏览,而不是像某些应用程序一样浏览。 但是,如果可以通过图形 API 或其他方式以我自己的身份进行浏览,请告知。

最佳答案

如果您多次执行此过程,Selenium 在 WebDriverWait 中有一些选项,可以避免浪费太多时间并检查元素是否可见。

Here是有关 Selenium 中 Waits 的文档以及 this是 Selenium 的 Python 文档中讨论预期条件类的部分。看来“可点击”是您可以检查的条件之一。

就我而言,我编写了一个简单的函数来处理可见性和单击,并在每次需要单击动态内容时调用它。

我的示例代码:

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

Browser = webdriver.Chrome()

def wait_until_visible_then_click(element):
    element = WebDriverWait(Browser,5,poll_frequency=.2).until(
        EC.visibility_of(element))
    element.click()

编辑:

上面的链接似乎被削弱了。 This是关于等待的新文档和 here是预期的条件文档。

关于python - Selenium -python 元素不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23857145/

相关文章:

python - Mac 操作系统 : How do I run a multi-line script on command line?

python - 运行时错误 : CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling `cublasSgemm( handle)` with GPU only

python - 在 python 中具有事件 TCP 连接的 HTTP 服务器

facebook - Facebook sdk 登录 Xcode 8 beta 中的 exc_bad_access

javascript - Div 覆盖空白区域和元素,因此看起来无法点击(Python Selenium)

java - 如何使用 Actions 类在 chrome 中打开新选项卡

python selenium 从下拉菜单中选择

python - python中具有多个匹配项和否定条件的多行正则表达式

javascript - Facebook JS (FBJS) 事件监听器

xcode - 登录后未调用Facebook ios6 completionHandler