Javascript 在Python中使用firefox webdriver滚动后,如何单击显示的按钮?

Javascript 在Python中使用firefox webdriver滚动后,如何单击显示的按钮?,javascript,python,html,Javascript,Python,Html,嗨,我的问题是:我需要滚动一页,在滚动结束时出现一个按钮,一个经典的“显示更多”按钮,当我点击这个按钮时,我需要重新滚动网页,直到到达网页的结尾。 到目前为止,我所做的是: from contextlib import closing import time from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver import Firefox #

嗨,我的问题是:我需要滚动一页,在滚动结束时出现一个按钮,一个经典的“显示更多”按钮,当我点击这个按钮时,我需要重新滚动网页,直到到达网页的结尾。 到目前为止,我所做的是:

from contextlib import closing

import time
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver import Firefox # pip install selenium
from selenium.webdriver.support.ui import WebDriverWait
url = 'https://play.google.com/store/apps/category/GAME_ARCADE/collection/topselling_free?hl=it'
# use firefox to get page with javascript generated content
SCROLL_PAUSE_TIME = 0.5

capabilities = DesiredCapabilities.FIREFOX
capabilities["marionette"] = True

# Get scroll height
with closing(Firefox(capabilities=capabilities)) as browser:
    browser.get(url)
    last_height = browser.execute_script("return document.body.scrollHeight")
    button = None
    while True:
        if (browser.find_element_by_id('show-more-button')):
            browser.find_element_by_id('show-more-button').click()
            #print("\n\nButton found = " + str(button) + "\n\n")

        # Scroll down to bottom
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")

        # Wait to load page
        time.sleep(SCROLL_PAUSE_TIME)

        # Calculate new scroll height and compare with last scroll height
        new_height = browser.execute_script("return document.body.scrollHeight")

        if new_height == last_height:
            break
        last_height = new_height
    # wait for the page to load

    # store it to string variable
    page_source = browser.page_source
print(page_source)
我想用这段代码检查页面中的按钮元素,如果还不存在,滚动页面。如果找到按钮元素,单击它,然后再次滚动页面“直到结束”。 此代码的问题是,当我尝试单击“查找”按钮时,我收到以下错误:

Traceback (most recent call last):
  File "/Users/gfuccio/PycharmProjects/htmlParsing/generatedHtmlParser.py", line 21, in <module>
    browser.find_element_by_id('show-more-button').click()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py", line 77, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py", line 493, in _execute
    return self._parent.execute(command, params)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message:
但结果总是false,尽管滚动后生成的html包含以下元素:

<button class="play-button" id="show-more-button" style="display:none">Mostra altro</button>
Mostra altro
我知道问题在于样式属性,但是对于javascript,我认为这个值已经改变了,你不这么认为吗?因为如果我滚动页面直到找到按钮,无论我是否检查页面,我都会发现以下情况:


如果看到,则显示属性设置为块。那么为什么在滚动之后,我得到了由javascript生成的新html,并且元素的“style”属性没有改变呢?

selenium文档解释了错误的含义:按钮存在,但隐藏在其他dom元素后面,因此您可以单击它。
<button class="play-button" id="show-more-button" style="display:none">Mostra altro</button>