Selenium python错误:元素无法滚动到视图中 [英] Selenium python Error: element could not be scrolled into view

查看:561
本文介绍了Selenium python错误:元素无法滚动到视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的公司自动化IdentiGO应用程序,但出现以下错误:

I am working on automating the IdentiGO application for my company, and I'm getting the following error:

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

这是我的代码,由于该脚本与我的问题无关,因此省略了指向该页面的脚本.

Here is my code, with the scripts leading up to this page omitted since they aren't relevant to my problem.

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()

在此代码之前的页面上,用户可以在其中选择约会日期和时间;我希望脚本等待按下开始"按钮,然后在以下屏幕截图中单击继续":

On the page prior to this code, where the user selects an appointment date and time; I want the script to wait for the "Go" button to be pushed, then click on "Continue" in the following screenshot:

如果您想查看确切的页面,请转至网址,然后您必须输入使用以下信息的一系列POST请求:

If you would like to see the exact page, go to this url, then you will have to make a series of POST requests using the following info:

  • 点击安排新约会
  • 其他
  • 供应商和承包商(儿童)
  • tnvc00047
  • 37204
  • 设定随机约会日期

任何建议都将不胜感激!

Any advice would really be appreciated!

以下是带有页面html的JS小提琴:

Here is a JS Fiddle with the html of the page:

https://jsfiddle.net/khf4tym3/

当我单击查看页面源代码"时,弹出HTML不会显示在源代码中,因此我认为它是用JS生成的.

When I click "view page source", the popup html doesn't show in the source code, so I assume that it is generated with 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>

更新2

如果将行WebDriverWait(driver, 1000000)更改为WebDriverWait(driver, 30),则会收到以下错误消息:

Update 2

If I change the line WebDriverWait(driver, 1000000) to WebDriverWait(driver, 30), I get the following error instead:

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: 

pythonfiddle

到目前为止,该项目的

代码,因此您可以跳过论坛条目.

pythonfiddle

code for the project so far, so you can skip the forum entry.

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()

推荐答案

此错误消息...

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

...表示 WebDriver 实例(即 driver )无法滚动

...implies that the WebDriver instance i.e. driver was unable to scroll the element within the Viewport to invoke click().

首先,因为您的用例是在元素上调用click(),所以理想情况下,不是使用presence_of_element_located(),而是需要使用 ExpectedConditions 作为 element_to_be_clickable() >如下:

First of all, as your usecase is to invoke click() on the element, ideally instead of using presence_of_element_located() you need to use the ExpectedConditions as element_to_be_clickable() as follows:

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

您可以在以下位置找到一些详细的讨论:

You can find a couple of detailed discussions in:

  • Message: Element could not be scrolled into view while trying to click on an option within a dropdown menu through Selenium
  • org.openqa.selenium.ElementNotInteractableException: Element could not be scrolled into view when trying to click a button

作为替代,根据错误消息,在调用click()之前在视口中滚动元素,您也可以使用

As an alternative, as per the error message, to scroll an element within the Viewport before invoking click() you can also use the Element.scrollIntoView() method.

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

You can find a detailed discussion in: - What is the difference between the different scroll options?

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

At this point it is worth to mention, the following methods:

  • move_to_element() from selenium.webdriver.common.action_chains
  • element_to_be_clickable() from selenium.webdriver.support.expected_conditions

将自动滚动 视口中的元素.

您可以在以下位置找到详细的讨论: -如何使用selenium webdriver在Python中滚动网页,而无需使用javascript方法execute_script()

You can find a detailed discussion in: - How to scroll a webpage using selenium webdriver in Python without using javascript method execute_script()

文本为继续的按钮位于顶级内容中,但在模态对话框中呈现.

The button with text as Continue is within the Top Level Content but rendered within a Modal Dialog Box.

DevTools快照:

DevTools Snapshot:

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

As the desired element is within a Modal Dialog Box, so to locate and invoke click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:

  • 使用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()

  • 注意:您必须添加以下导入:

  • Note : You have to add the following imports :

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

  • DevTools快照:

    DevTools Snapshot:

    这篇关于Selenium python错误:元素无法滚动到视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆