51Testing软件测试论坛

标题: selenium点击后出现页面自动刷新现象问题求助 [打印本页]

作者: ira    时间: 2021-1-9 10:59
标题: selenium点击后出现页面自动刷新现象问题求助
本帖最后由 ira 于 2021-1-9 11:20 编辑

问题详细描述:selenium,click点击超链接的时候,新窗口打开页面,新窗口和老窗口会出现自动刷新现象,导致脚本无法运行。

点击的元素是这个:<a href="/zh-cn/info/" target="_blank" rel="noopener noreferrer">订单</a>
目前调试发现比较关键的一段代码:actions = ActionChains(driver) # chrome加了这句就不会自动刷新,firefox加了还是会刷新
请问一下各位有没有出现过这类问题,但是add_argument('headless')的模式运行就不会出现问题,case能顺利跑完。
有没有大神出来分析下,非常感谢~~~~

————————————华丽的分割线————————————
代码如下:
driver.maximize_window()
log.logger.info('打开网站并等待5s')
url1 = ''
driver.get(url1)
log.logger.info('打开完成')
time.sleep(5)
futures_index = driver.window_handles[0]
actions = ActionChains(driver)

class index(unittest.TestCase):

        # @unittest.skip(" test")
        def test_5_header_info2(self):
                # 头部
                # driver.get(url1)  # 调试注释
                # time.sleep(5)  # 调试注释
                for i in range(0,6):
                        element_class_name_parent = 'header-left-box'
                        element_parent = driver.find_element_by_class_name(element_class_name_parent)
                        elements_class_name = 'header-left-link'
                        headers_elements = element_parent.find_elements_by_class_name(elements_class_name)
                        print(headers_elements[6])
                        actions = ActionChains(driver) #chrome加了这句就不会自动刷新,firefox加了还是会刷新
                        actions.move_to_element(headers_elements[6]).perform()
                        time.sleep(1)
                        # elements_xpath_name = "//li[@class='header-left-item    zh-CN'][2]/div[@class='header-hover-modal']/ul[@class='header-hover-modal-list']/li"
                        elements_xpath_name = "//li[@class='header-left-item    zh-CN'][2]/div[@class='header-hover-modal']/ul[@class='header-hover-modal-list']/li"
                        elements = element_parent.find_elements_by_xpath(elements_xpath_name)
                        elements.find_element_by_tag_name("a").click()
                        time.sleep(1)
                        driver.switch_to.window(futures_index)  # 切换回窗口
                        time.sleep(2)
                        time.sleep(5)


chrome不加actions = ActionChains(driver)的错误内容:
    actions.move_to_element(headers_elements[6]).perform()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/common/action_chains.py", line 80, in perform
    self.w3c_actions.perform()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/common/actions/action_builder.py", line 76, in perform
    self.driver.execute(Command.W3C_ACTIONS, enc)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=87.0.4280.88)
firefox加actions = ActionChains(driver)的错误内容:

    elements.find_element_by_tag_name("a").click()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: Element could not be scrolled into view

firefox不加actions = ActionChains(driver)的错误内容:
actions.move_to_element(headers_elements[6]).perform()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/common/action_chains.py", line 80, in perform
    self.w3c_actions.perform()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/common/actions/action_builder.py", line 76, in perform
    self.driver.execute(Command.W3C_ACTIONS, enc)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: The element reference of xx is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed







作者: ira    时间: 2021-1-9 11:21
解决有大红包
作者: 赵佳乐SMILE    时间: 2021-1-14 08:37
没遇到过




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2