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

ham*_*yad 2 selenium xpath href python-3.x webscarab

如果我的代码是这样的话:

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

但是,发生此错误:

selenium.common.exceptions.ElementNotInteractableException: Message: Element <a href="/p/BgEcF34Fqf6/?tagged=fast"> could not be scrolled into view
Run Code Online (Sandbox Code Playgroud)

请问你能帮帮我吗?

Gir*_*shB 6

试用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)
Run Code Online (Sandbox Code Playgroud)