点击Selenium + Python无法滚动到视图的链接的方式是什么?

What is the way of clicking on links which could not be scrolled into view by Selenium + Python?

如果我的代码是这样的:

x=driver.find_element_by_xpath("""//*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[2]/a""")
x.click()

但是,出现了这个错误:

selenium.common.exceptions.ElementNotInteractableException: Message: Element <a href="/p/BgEcF34Fqf6/?tagged=fast"> could not be scrolled into view

你能帮帮我吗?

尝试JavaScript点击:

from selenium.webdriver.remote.webelement import WebElement


def javascript_click(self, locator):
    element = None
    if isinstance(locator, str):
        element = self.find_element(locator)
    elif isinstance(locator, WebElement):
        element = locator

    if element is not None:
        self._driver.execute_script("arguments[0].click();", element)
    else:
        raise Exception("Could not click on locator " + element)

我使用了这个经过精心设计的代码:

#Scroll down the page to load more posts
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(10)
    j=j+1

请注意,您可以使用 for 语法将此滚动概括为多个信誉。