当前位置: 首页 > 知识库问答 >
问题:

Selenium python错误:元素无法滚动到视图中

祝俊
2023-03-14

我正在为我的公司自动化IdentiGO应用程序,并得到以下错误:

Internal Server Error: /identigo
Traceback (most recent call last):
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/jane/Code/maynard_env/maynard/employee/views.py", line 63, in post
    driver.main(employee)
  File "/Users/jane/Code/maynard_env/maynard/employee/driver.py", line 31, in main
    WebDriverWait(driver, 1000000).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[5]/div[3]/div/button/span'))).click()
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Users/jane/Code/maynard_env/env/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 <span class="ui-button-text"> could not be scrolled into view

下面是我的代码,由于与我的问题无关,所以省略了导致该页面的脚本。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


WebDriverWait(driver, 1000000).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[5]/div[3]/div/button/span'))).click()

在此代码之前的页面上,用户选择约会日期和时间;我希望脚本等待“开始”按钮被推,然后在下面的截图中点击“继续”:

    null

https://jsfiddle.net/khf4tym3/

当我单击“View page source”时,弹出的html不会显示在源代码中,所以我假设它是用JS生成的。

<div class="ui-dialog-buttonset">
    <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
        <span class="ui-button-text">Continue</span>
    </button>
</div>

如果将webdriverwait(driver,1000000)行更改为webdriverwait(driver,30),则会出现以下错误:

Internal Server Error: /identigo
Traceback (most recent call last):
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/jane/Code/maynard_env/maynard/employee/views.py", line 63, in post
    driver.main(employee)
  File "/Users/jane/Code/maynard_env/maynard/employee/driver.py", line 34, in main
    element=WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='ui-dialog-buttonset']/button[@class='ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only']/span[contains(.,'Continue')]")))
  File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

到目前为止的项目代码,所以您可以跳过论坛条目。

https://jsfiddle.net/93k5s2xg/1/

工作解决方案:

WebDriverWait(driver, 20).until(expected_conditions.element_to_be_clickable((By.XPATH, "//div[starts-with(@aria-describedby, 'ui-id-')]//span[@class='ui-button-text' and text()='Continue']"))).click()

共有1个答案

鲜于宜修
2023-03-14

此错误消息...

selenium.common.exceptions.ElementNotInteractableException: Message: Element <span class="ui-button-text"> could not be scrolled into view

...暗示WebDriver实例(即driver)无法在视口内滚动元素以调用click()

首先,由于您的习惯是在元素上调用click(),因此理想情况下,您需要将ExpectedConditions用作element_to_be_clickable(),而不是使用presence_of_element_locited():

WebDriverWait(driver, 1000000).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[5]/div[3]/div/button/span'))).click()
    null

另外,根据错误消息,要在调用click()之前在视口中滚动元素,还可以使用element.scrollintoView()方法。

您可以在下面找到详细的讨论:-不同滚动选项之间的区别是什么?

在这一点上值得一提的是,以下方法:

    null

DevTools快照:

由于所需的元素位于模态对话框中,因此要在元素上找到并调用click(),您必须为element_to_be_clickable()导入WebDriverWait,您可以使用以下定位器策略:

>

  • 使用XPath:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(@aria-describedby, 'ui-id-')]//span[@class='ui-button-text' and text()='Continue']"))).click()
    
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

  •  类似资料:
    • 我正在尝试用Selenium选择一个值。 你能帮我纠正一下情况吗?

    • 我正在尝试使用java和Eclipse学习Selenium。我试图找到网页中的一个元素。 我尝试使用这行代码单击一个html元素: 下图显示了页面的html结构:

    • 有人能帮我一下吗:检查元素代码是。 代码为:

    • 我使用Selenium在一个有多个下拉菜单的页面上运行测试(特别是一个弹出页面,允许您选择一些选项然后关闭它)。我可以点击这些菜单中的一些完全罚款;但是,其中一些会抛出异常,消息为“Element不能滚动到视图中”,即使菜单是紧挨着的。我很困惑为什么一个菜单有效,而另一个却不有效,尽管它们看起来是一样的。为了点击菜单,我尝试了三种方法: a)常规硒点击: 这是其他菜单的工作原理,除了我直接导航到“

    • 问题内容: 尝试使用selenium-webdriver python向下滚动到页面底部,以便加载更多产品。 该网页已加载,但没有变化。 我想念什么吗? 问题答案: 您可以尝试以下move_up和move_down函数:

    • 我在这里已经研究了这个问题:调试“Element is not clickable at point”错误和Selenium webdriver无法单击页面外的链接,该链接建议使用javascript滚动元素。 谢谢,希望这有点清楚。