selenium.common.exceptions.ElementNotVisibleException:消息:元素不可交互

时间:2018-10-27 18:42:39

标签: python facebook selenium

我正在使用此代码自动将帖子发布到fb组,但收到此错误消息。

  File "fbposter.py", line 87, in <module>
    main()
  File "fbposter.py", line 81, in main
    post_button.click()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
  (Session info: chrome=69.0.3497.100)
  (Driver info: chromedriver=2.42.591059 (a3d9684d10d61aa0c45f6723b327283be1ebaad8),platform=Mac OS X 10.13.6 x86_64)

代码

post_button = driver.find_element_by_xpath("//*[@data-testid='react-composer-post-button']")
        clickable = False
        while not clickable:
            cursor = post_button.find_element_by_tag_name('span').value_of_css_property("cursor")
            if cursor == "pointer":
                clickable = True
                break
        post_button.click()
        sleep(5)

FB论坛帖子上的HTML代码

<button class="_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft" data-testid="react-composer-post-button" type="submit" value="1"><div alt="" class="_3-8_ _2ph- _6q3h"><span class="img _55ym _55yn _55yp" role="progressbar" aria-valuetext="Loading..." aria-busy="true" aria-valuemin="0" aria-valuemax="100"></span></div><span class="">Post</span></button>

参考:https://github.com/lalongooo/selenium-fb-group-poster

1 个答案:

答案 0 :(得分:0)

我已经找到答案了。

        sleep(5)
        buttons = driver.find_elements_by_tag_name("button")
        sleep(5)
        for button in buttons:
            if button.text == "Post":
                button.click()