【问题标题】:Selenium python Error: element could not be scrolled into viewSelenium python错误:元素无法滚动到视图中
【发布时间】:2019-09-28 19:24:43
【问题描述】:

我正在为我的公司自动化 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()

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

如果您想查看确切的页面,请转到this url,然后您必须使用以下信息发出一系列 POST 请求:

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

任何建议都将不胜感激!

更新

这是一个带有页面 html 的 JS Fiddle:

https://jsfiddle.net/khf4tym3/

当我点击“查看页面源代码”时,弹出的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>

更新 2

如果我将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: 

pythonfiddle

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

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

【问题讨论】:

  • 你能发布那个警报的 html 吗?
  • 要么你必须分享 html 或正确的 tvnc。
  • @CalebGoodman : 抱歉,我没有看到你的消息。请检查..给我一些时间,我会回复你
  • 让我知道它是怎么回事。我已经在 chrome 浏览器上进行了测试。如果你愿意,我明天可以与你分享完整的代码,它可以在我的 Windows 笔记本电脑上正常工作。
  • @KunduK 我添加了另一个赏金,让您值得花时间。

标签: python selenium firefox geckodriver webdriverwait


【解决方案1】:

此错误消息...

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

...暗示 WebDriver 实例,即 driver 无法滚动 Viewport 中的元素以调用 click()


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

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

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


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

您可以在以下位置找到详细讨论: - What is the difference between the different scroll options?


此时值得一提的是,以下方法:

会自动滚动视口中的元素。

您可以在以下位置找到详细讨论: - How to scroll a webpage using selenium webdriver in Python without using javascript method execute_script()


这个用例

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

DevTools 快照:

由于所需的元素位于 模态对话框 中,因此要在元素上定位和调用 click(),您必须为 element_to_be_clickable() 诱导 WebDriverWait 和您可以使用以下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()
    
  • 注意:您必须添加以下导入:

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

DevTools 快照:

【讨论】:

    【解决方案2】:

    使用下面的 xpath 并点击它。

    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')]")))
    element.click()
    

    如果上面的点击不起作用,那么试试下面的。

    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')]")))
    element.location_once_scrolled_into_view
    element.click()
    

    或者你可以使用javascripts执行器来点击。

    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')]")))
    driver.execute_script("arguments[0].click();", element)
    

    EDITED

    试试下面的代码,它点击继续按钮,那里有继续和取消按钮。一旦你点击继续,你会点击另一个继续按钮。我已经从计划应用程序更新了代码。

    #Schedule appointment
    ele1=WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'(//button[@class="jquiButton ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"]/span[contains(.,"Schedule")])[1]')))
    driver.execute_script("arguments[0].click();",ele1)
    #click on continue button
    WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@class="twoButton continueButton ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"]/span[contains(.,"Continue")]'))).click()
    #click on second continue button
    WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"]/span[contains(.,"Continue")]'))).click()
    

    已编辑其余代码。

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium import webdriver
    from selenium.webdriver.support.select import Select
    import time
    
    driver=webdriver.Chrome()
    driver.get("https://tn.ibtfingerprint.com/")
    driver.maximize_window()
    WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@name="IN_PublicMenuSelection"]/span[contains(.,"Schedule a New Appointment")]'))).click()
    time.sleep(5)
    select=Select(driver.find_element_by_id("varAgency"))
    select.select_by_value("OTHR")
    WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@id="collectAgency"]/span[contains(.,"Go")]'))).click()
    element=WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.ID,'varAppType')))
    select=Select(element)
    select.select_by_value("60")
    WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@id="collectAppType"][contains(.,"Go")]'))).click()
    time.sleep(10)
    
    driver.find_element_by_id("varORI").send_keys("tnvc00047")
    WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@id="collectORI"][contains(.,"Go")]'))).click()
    WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@class="twoButton continueButton ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"]/span[contains(.,"yes")]'))).click()
    
    elements=WebDriverWait(driver,40).until(expected_conditions.presence_of_all_elements_located((By.XPATH,'(//i[@class="icon checkbox fa fa-fw fa-square-o fa-2x"])[last()]')))
    if(len(elements)>0):
       element=driver.find_element_by_xpath('(//div[@class="fieldentity"]//i[@class="icon checkbox fa fa-fw fa-square-o fa-2x"])[last()]')
       element.location_once_scrolled_into_view
       ActionChains(driver).move_to_element(element).click().perform()
       elements[0].click()
       driver.find_element_by_css_selector("div.fieldentity div").click()
       driver.execute_script("arguments[0].click();",element)
       element1=WebDriverWait(driver, 40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@class="jquiButton ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"]/span[contains(.,"Go")]')))
       driver.execute_script("arguments[0].click();", element1)
    
    time.sleep(10)
    driver.find_element_by_name("IN_varLocZipCode").send_keys("37204")
    WebDriverWait(driver,40).until(expected_conditions.presence_of_element_located((By.XPATH,'//button[@class="jquiButton ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"]/span[contains(.,"Go")]'))).click()
    ele1=WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'(//button[@class="jquiButton ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"]/span[contains(.,"Schedule")])[1]')))
    driver.execute_script("arguments[0].click();",ele1)
    time.sleep(10)
    WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@class="twoButton continueButton ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"]/span[contains(.,"Continue")]'))).click()
    WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"]/span[contains(.,"Continue")]'))).click()
    

    此代码在 chrome 浏览器和 windows 10 操作系统上运行良好。我已经测试了几次。


    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions
    from selenium import webdriver
    from selenium.webdriver.support.select import Select
    
    
    driver=webdriver.Chrome()
    driver.get("https://tn.ibtfingerprint.com/")
    driver.maximize_window()
    WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@name="IN_PublicMenuSelection"]/span[contains(.,"Schedule a New Appointment")]'))).click()
    element=WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.ID,'varAgency')))
    select=Select(element)
    select.select_by_value("OTHR")
    WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@id="collectAgency"]/span[contains(.,"Go")]'))).click()
    element=WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.ID,'varAppType')))
    select=Select(element)
    select.select_by_value("60")
    WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@id="collectAppType"][contains(.,"Go")]'))).click()
    
    WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.ID,'varORI'))).send_keys("tnvc00047")
    
    WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@id="collectORI"][contains(.,"Go")]'))).click()
    WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@class="twoButton continueButton ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"]/span[contains(.,"yes")]'))).click()
    
    
    elements=WebDriverWait(driver,40).until(expected_conditions.presence_of_all_elements_located((By.XPATH,'(//form[@id="cjisAcknowledgementForm"]//div[@class="fieldentity"]//i[@class="icon checkbox fa fa-fw fa-square-o fa-2x"])[last()]')))
    
    if(len(elements)>0):
       element=driver.find_element_by_xpath('(//form[@id="cjisAcknowledgementForm"]//div[@class="fieldentity"]//i[@class="icon checkbox fa fa-fw fa-square-o fa-2x"])[last()]')
       driver.execute_script("arguments[0].click();",element)
       element1=WebDriverWait(driver, 40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@class="jquiButton ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"]/span[contains(.,"Go")]')))
       driver.execute_script("arguments[0].click();", element1)
    
    WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.NAME,'IN_varLocZipCode'))).send_keys("37204")
    
    WebDriverWait(driver,40).until(expected_conditions.presence_of_element_located((By.XPATH,'//button[@class="jquiButton ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"]/span[contains(.,"Go")]'))).click()
    ele1=WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'(//button[@class="jquiButton ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"]/span[contains(.,"Schedule")])[1]')))
    driver.execute_script("arguments[0].click();",ele1)
    
    WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@class="twoButton continueButton ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"]/span[contains(.,"Continue")]'))).click()
    WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"]/span[contains(.,"Continue")]'))).click()
    

    【讨论】:

    • 我都收到了selenium.common.exceptions.TimeoutException: Message: 错误。我添加了带有完整回溯的更新。感谢您的帮助!
    • 你能检查一下你发布的那个html页面上是否有可用的iframe吗?
    • 我检查了inspector,没有找到。
    • 好的,使用你的 xpath 并使用第二个或第三个选项来检查它是否有效?
    • 我尝试使用我的 xpath,但仍然得到 TimeoutException。当我将等待时间设置为 30 时,我得到一个超时,当我将它设置为 10,000 时,我得到原始错误消息。
    猜你喜欢
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 2019-08-15
    相关资源
    最近更新 更多