51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2701|回复: 2
打印 上一主题 下一主题

[求助] selenium点击后出现页面自动刷新现象问题求助

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2021-1-9 10:59:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 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






分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
 楼主| 发表于 2021-1-9 11:21:12 | 只看该作者
解决有大红包
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-11-24 11:31 , Processed in 0.065003 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表