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

ig9co6j1  于 2023-06-23  发布在  Python
关注(0)|答案(4)|浏览(156)

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

  1. Internal Server Error: /identigo
  2. Traceback (most recent call last):
  3. File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
  4. response = get_response(request)
  5. File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
  6. response = self.process_exception_by_middleware(e, request)
  7. File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
  8. response = wrapped_callback(request, *callback_args, **callback_kwargs)
  9. File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
  10. return self.dispatch(request, *args, **kwargs)
  11. File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
  12. return handler(request, *args, **kwargs)
  13. File "/Users/jane/Code/maynard_env/maynard/employee/views.py", line 63, in post
  14. driver.main(employee)
  15. File "/Users/jane/Code/maynard_env/maynard/employee/driver.py", line 31, in main
  16. WebDriverWait(driver, 1000000).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[5]/div[3]/div/button/span'))).click()
  17. File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
  18. self._execute(Command.CLICK_ELEMENT)
  19. File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
  20. return self._parent.execute(command, params)
  21. File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
  22. self.error_handler.check_response(response)
  23. File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
  24. raise exception_class(message, screen, stacktrace)
  25. selenium.common.exceptions.ElementNotInteractableException: Message: Element <span class="ui-button-text"> could not be scrolled into view

下面是我的代码,省略了导致这个页面的脚本,因为它们与我的问题无关。

  1. from selenium import webdriver
  2. from selenium.webdriver.support.ui import WebDriverWait
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.support import expected_conditions as EC
  5. 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
  • 随机预约日期

任何建议将真的很感激!

更新

下面是一个JS Fiddle与页面的html:
https://jsfiddle.net/khf4tym3/
当我点击“查看页面源代码”时,弹出的html并没有显示在源代码中,所以我假设它是用JS生成的。

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

更新2

如果我将WebDriverWait(driver, 1000000)改为WebDriverWait(driver, 30),我会得到以下错误:

  1. Internal Server Error: /identigo
  2. Traceback (most recent call last):
  3. File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
  4. response = get_response(request)
  5. File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
  6. response = self.process_exception_by_middleware(e, request)
  7. File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
  8. response = wrapped_callback(request, *callback_args, **callback_kwargs)
  9. File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
  10. return self.dispatch(request, *args, **kwargs)
  11. File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
  12. return handler(request, *args, **kwargs)
  13. File "/Users/jane/Code/maynard_env/maynard/employee/views.py", line 63, in post
  14. driver.main(employee)
  15. File "/Users/jane/Code/maynard_env/maynard/employee/driver.py", line 34, in main
  16. 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')]")))
  17. File "/Users/jane/Code/maynard_env/env/lib/python3.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
  18. raise TimeoutException(message, screen, stacktrace)
  19. selenium.common.exceptions.TimeoutException: Message:

Python小提琴

代码的项目,所以你可以跳过论坛条目。
https://jsfiddle.net/93k5s2xg/1/
工作溶液:

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

lymnna711#

此错误消息...

  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(),如下所示:

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

您可以在以下内容中找到一些详细的讨论:

  • 消息:当尝试通过Selenium单击下拉菜单中的选项时,无法将元素滚动到视图中
  • org.openqa.selenium.ElementNotInteractableException:尝试单击按钮时,无法将元素滚动到视图中

作为替代,根据错误消息,在调用click()之前,您也可以使用Element.scrollIntoView()方法来滚动Viewport中的元素。
您可以在以下内容中找到详细的讨论:- 不同的滚动选项之间有什么区别?
在这一点上值得一提的是,以下方法:

  • 从selenium.webdriver.common.action_chains中移动到元素
  • selenium.webdriver.support.expected_conditions中的element_to_be_clickable()

将自动滚动**Viewport 中的元素。
您可以在以下内容中找到详细的讨论:- 如何在Python中使用selenium webdriver滚动网页,而不使用JavaScript方法execute_script()

本用例

文本为Continue的按钮位于 Top Level Content 中,但在 Modal Dialog Box 中呈现。
DevTools快照:

由于所需的元素位于 Modal Dialog Box 中,因此要在元素上定位和调用click(),您必须为element_to_be_clickable()引入 WebDriverWait,您可以使用以下定位器策略:

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

*注意:需要添加以下导入:

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

DevTools快照:

展开查看全部
dzhpxtsq

dzhpxtsq2#

使用下面的xpath并单击它。

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

如果上面的点击不工作,然后尝试下面的一个。

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

或者你可以使用javascript executor来点击。

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

尝试下面的代码,它是点击继续按钮,其中既继续和取消按钮在那里。一旦你点击继续,你将另一个继续按钮点击。代码我已经从日程表应用程序更新。

  1. #Schedule appointment
  2. 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]')))
  3. driver.execute_script("arguments[0].click();",ele1)
  4. #click on continue button
  5. 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()
  6. #click on second continue button
  7. 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()
    • 已编辑其余代码。**
  1. from selenium.webdriver.common.by import By
  2. from selenium.webdriver.support.ui import WebDriverWait
  3. from selenium.webdriver.support import expected_conditions
  4. from selenium.webdriver.common.action_chains import ActionChains
  5. from selenium import webdriver
  6. from selenium.webdriver.support.select import Select
  7. import time
  8. driver=webdriver.Chrome()
  9. driver.get("https://tn.ibtfingerprint.com/")
  10. driver.maximize_window()
  11. WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@name="IN_PublicMenuSelection"]/span[contains(.,"Schedule a New Appointment")]'))).click()
  12. time.sleep(5)
  13. select=Select(driver.find_element_by_id("varAgency"))
  14. select.select_by_value("OTHR")
  15. WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@id="collectAgency"]/span[contains(.,"Go")]'))).click()
  16. element=WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.ID,'varAppType')))
  17. select=Select(element)
  18. select.select_by_value("60")
  19. WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@id="collectAppType"][contains(.,"Go")]'))).click()
  20. time.sleep(10)
  21. driver.find_element_by_id("varORI").send_keys("tnvc00047")
  22. WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@id="collectORI"][contains(.,"Go")]'))).click()
  23. 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()
  24. 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()]')))
  25. if(len(elements)>0):
  26. element=driver.find_element_by_xpath('(//div[@class="fieldentity"]//i[@class="icon checkbox fa fa-fw fa-square-o fa-2x"])[last()]')
  27. element.location_once_scrolled_into_view
  28. ActionChains(driver).move_to_element(element).click().perform()
  29. elements[0].click()
  30. driver.find_element_by_css_selector("div.fieldentity div").click()
  31. driver.execute_script("arguments[0].click();",element)
  32. 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")]')))
  33. driver.execute_script("arguments[0].click();", element1)
  34. time.sleep(10)
  35. driver.find_element_by_name("IN_varLocZipCode").send_keys("37204")
  36. 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()
  37. 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]')))
  38. driver.execute_script("arguments[0].click();",ele1)
  39. time.sleep(10)
  40. 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()
  41. 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操作系统上运行良好。我已经测试了几次。

  1. from selenium.webdriver.common.by import By
  2. from selenium.webdriver.support.ui import WebDriverWait
  3. from selenium.webdriver.support import expected_conditions
  4. from selenium import webdriver
  5. from selenium.webdriver.support.select import Select
  6. driver=webdriver.Chrome()
  7. driver.get("https://tn.ibtfingerprint.com/")
  8. driver.maximize_window()
  9. WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@name="IN_PublicMenuSelection"]/span[contains(.,"Schedule a New Appointment")]'))).click()
  10. element=WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.ID,'varAgency')))
  11. select=Select(element)
  12. select.select_by_value("OTHR")
  13. WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@id="collectAgency"]/span[contains(.,"Go")]'))).click()
  14. element=WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.ID,'varAppType')))
  15. select=Select(element)
  16. select.select_by_value("60")
  17. WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@id="collectAppType"][contains(.,"Go")]'))).click()
  18. WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.ID,'varORI'))).send_keys("tnvc00047")
  19. WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.XPATH,'//button[@id="collectORI"][contains(.,"Go")]'))).click()
  20. 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()
  21. 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()]')))
  22. if(len(elements)>0):
  23. 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()]')
  24. driver.execute_script("arguments[0].click();",element)
  25. 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")]')))
  26. driver.execute_script("arguments[0].click();", element1)
  27. WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.NAME,'IN_varLocZipCode'))).send_keys("37204")
  28. 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()
  29. 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]')))
  30. driver.execute_script("arguments[0].click();",ele1)
  31. 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()
  32. 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()
展开查看全部
bfrts1fy

bfrts1fy3#

如果有人仍然有这个问题,我通过设置窗口大小来修复它。Java中的例子:driver.manage().window().setSize(new Dimension(5120, 2880));

jfewjypa

jfewjypa4#

为了解决这个问题,我只是在我的WebDriverManager类中为所有测试添加了这一行:getWebDriver().manage().window().maximize();

相关问题