python 中的硒:click() 在方法内不起作用

selenium in python: click() doesn't work within method

我正在考虑一种连续向下滚动 138px 直到属性出现并被点击的方法。

不用方法也能正常工作:

div_height = 138
total_height = div_height
while True:
    try:
        driver.find_element_by_xpath("//div[@class='item']").click()
        break
    except:
        driver.execute_script(
        "document.querySelector('div.result').scrollTo(0, " + str(total_height) + ");"
            )
        total_height += div_height

但是如果我尝试通过方法发送驱动程序,它就不起作用。看看:

def scroll_down_until_done(action, selectors):
    div_height = 138
    total_height = div_height
    while True:
        try:
            action
            break
        except:
            driver.execute_script(
                "document.querySelector(\'" + str(selectors) + "\').scrollTo(0, " + str(total_height) + ");"
            )
            total_height += div_height

scroll_down_until_done(driver.find_element_by_xpath("//div[@class='item']").click(), "div.result")

然后显示错误:

Traceback (most recent call last):
  File "/Users/electronyoon/Documents/GitHub/crawler/com/cralwer.py", line 203, in <module>
    parser()
  File "/Users/electronyoon/Documents/GitHub/crawler/com/cralwer.py", line 197, in parser
    active_and_expired_manager()
  File "/Users/electronyoon/Documents/GitHub/crawler/com/cralwer.py", line 142, in active_and_expired_manager
    scroll_down_until_done(driver.find_elements_by_xpath("//div[@class='apt-item']")[4].click(), "div.result")
  File "/Users/electronyoon/Documents/GitHub/crawler/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/Users/electronyoon/Documents/GitHub/crawler/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "/Users/electronyoon/Documents/GitHub/crawler/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Users/electronyoon/Documents/GitHub/crawler/venv/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=75.0.3770.142)

我很难理解其中的区别。有帮助吗?

新人。在上面的评论的帮助下,我重新制作了。 我改为发送一个网络元素并点击它。

def scroll_down_until_done(element, selectors):
    div_height = 138
    total_height = div_height
    while True:
        try:
            element.click()
            break
        except:
            driver.execute_script(
                "document.querySelector(\'" + str(selectors) + "\').scrollTo(0, " + str(total_height) + ");"
            )
            total_height += div_height

element = driver.find_elements_by_xpath("//div[@class='item']")
scroll_down_until_done(element, "div.result")

希望这对其他人有所帮助。

两个代码片段不一样。 scroll_down_until_done() 实际上会触发 driver.find_element_by_xpath(),之后甚至会在进入 scroll_down_until_done 之前触发 click()。您需要发送函数的名称并使用 action()

调用它
def scroll_down_until_done(action, selector_to_click, selectors):
    div_height = 138
    total_height = div_height
    while True:
        try:
            action(selector_to_click).click()
            break
        except:
            driver.execute_script(
                "document.querySelector(\'" + str(selectors) + "\').scrollTo(0, " + str(total_height) + ");"
            )
            total_height += div_height

scroll_down_until_done(driver.find_element_by_xpath, "//div[@class='item']", "div.result")