51Testing
怬
µçÄÔ°æ

java+selenium Ìø¹ý»¬¿éÍê³ÉÒ³ÃæÌøת

2024-4-01 09:16×÷ÕߣºØýÃûÀ´Ô´£º51CTO²©¿Í

¡¡¡¡ÔÚ×öweb×Ô¶¯»¯µÄʱºò£¬¾­³£»áÓöµ½µã»÷Ò»¸öÔªËص¯³öÁËÒ»¸öеĴ°¿Ú£¬ÎÒÃÇÐèÒª½øÈ뵽еĴ°¿ÚÖУ¬½øÐÐ×Ô¶¯»¯²Ù×÷£¬ÄÇôÎÒÃÇÈçºÎÌøתµ½ÐµĴ°¿ÚÉÏÄØ£¿
¡¡¡¡Çл»Ð´°¿Ú
¡¡¡¡ÔÚä¯ÀÀÆ÷ÉÏÓöµ½ÐµĴ°¿Ú´ò¿ªµÄʱºò£¬ÒòΪ½Å±¾²»ÖªµÀÎÒÃÇÏëÒªÌøתµ½ÄǸö´°¿ÚÉÏ£¬ÏÈ»ñÈ¡È«²¿´°¿ÚÐÅÏ¢£¬È»ºó½øÐÐÑ¡Ôñ´°¿Ú½øÐÐÌøת£¬ÕâÀï´°¿ÚÊÇͨ¹ý¾ä±úµÄ·½·¨½øÐÐʶ±ðµÄ¡£ÕâÀï¾ÍÓõ½ÁËseleniumÖеÄ3¸ö·½·¨£¬»ñÈ¡µ±Ç°´°¿Ú¾ä±úµÄ·½·¨£¬ºÍ»ñÈ¡È«²¿´°´°¿Ú¾ä±úµÄ·½·¨£¬ÒÔ¼°Ìøתµ½´°¿Ú¾ä±úµÄ·½·¨¡£
¡¡¡¡»ñÈ¡µ±Ç°´°¿Ú¾ä±ú
¡¡¡¡Í¨¹ý·½·¨£ºcurrent_window_handle
¡¡¡¡Ô´Â룺
¡¡¡¡@property
¡¡¡¡    def current_window_handle(self):
¡¡¡¡        """
¡¡¡¡        Returns the handle of the current window.
¡¡¡¡        :Usage:
¡¡¡¡            driver.current_window_handle
¡¡¡¡        """
¡¡¡¡        if self.w3c:
¡¡¡¡            return self.execute(Command.W3C_GET_CURRENT_WINDOW_HANDLE)['value']
¡¡¡¡        else:
¡¡¡¡            return self.execute(Command.GET_CURRENT_WINDOW_HANDLE)['value']
¡¡¡¡»ñÈ¡È«²¿´°¿Ú¾ä±ú
¡¡¡¡Í¨¹ý·½·¨£º?window_handles
¡¡¡¡Ö´ÐÐÍê½á¹ûΪÁбíÏÔʾÄÚÈÝ£¬×îºóÒ»¸öµ¯³ö¿òΪ×îºóÒ»¸ö¡£
¡¡¡¡Ô´Â룺
¡¡¡¡@property
¡¡¡¡    def window_handles(self):
¡¡¡¡        """
¡¡¡¡        Returns the handles of all windows within the current session.
¡¡¡¡        :Usage:
¡¡¡¡            driver.window_handles
¡¡¡¡        """
¡¡¡¡        if self.w3c:
¡¡¡¡            return self.execute(Command.W3C_GET_WINDOW_HANDLES)['value']
¡¡¡¡        else:
¡¡¡¡            return self.execute(Command.GET_WINDOW_HANDLES)['value']
¡¡¡¡Ìøתµ½¶ÔÓ¦µÄ´°¿Ú
¡¡¡¡ÈçºÎ²é¿´´°¿ÚµÄ¾ä±úÒѾ­Á˽⵽ÁË£¬ÈçºÎÌøתµ½¶ÔÓ¦µÄ¾ä±úÉÏÄØ£¿ÎÒÃÇ¿ÉÒÔͨ¹ý·½·¨?switch_to.window()
¡¡¡¡switch_to_window()ºÍÇ°Ãæ˵µÄiframe·½·¨Ò»Ñù£¬¹Ù·½²»ÍƼöʹÓã¬ÎÒÃÇ¿ÉÒÔͨ¹ýswitch_to.window()·½·¨À´²Ù×÷¡£
¡¡¡¡Ô´Â룺
¡¡¡¡def switch_to_window(self, window_name):
¡¡¡¡        """ Deprecated use driver.switch_to.window
¡¡¡¡        """
¡¡¡¡        warnings.warn("use driver.switch_to.window instead",
¡¡¡¡                      DeprecationWarning, stacklevel=2)
¡¡¡¡        self._switch_to.window(window_name)
¡¡¡¡Í¨¹ýÉÏÃæµÄС°¸Àý·¢ÏÖ£¬ÒѾ­´Ó°Ù¶ÈÌøתµ½Á˵Ĵ°¿ÚÉÏ¡£ÕâÀïÓеãÎÊÌ⣬Èç¹ûµ±Ç°ÒѾ­ÓÐ2¸ö´°¿ÚÁË£¬ÎÒÃÇͨ¹ýϱê1¾Í²»ÄÜÌøתµ½×îеĴ°¿ÚÉÏ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýϱê-1µÄ·½·¨£¬Ã¿´Î¶¼ÕÒ×îºóÒ»¸ö£¬ÕâÀï×îºÃÒ»¸ö×ÜÊÇеĴ°¿Ú¡£
¡¡¡¡Í¨¹ýÏÔʾµÈ´ýÅжϴ°¿ÚÊÇ·ñ³öÏÖ
¡¡¡¡ÎÒÃÇ¿ÉÒÔͨ¹ýÏÔʾµÈ´ýµÄ·½·¨¼ÓÉÏÅжϴ°¿ÚÊÇ·ñ³öÏֵķ½·¨½øÐÐÀ´¼õÉÙÎÒÃǵÄÔËÐÐʱ¼äºÍ±ÜÃâÔËÐдíÎó¡£
¡¡¡¡Åжϴ°¿Ú³öÏÖ·½·¨Ç°ÃæÓÐ?new_window_is_opened()
¡¡¡¡Ô´Â룺
¡¡¡¡class new_window_is_opened(object):
¡¡¡¡    """ An expectation that a new window will be opened and have the number of
¡¡¡¡    windows handles increase"""
¡¡¡¡    def __init__(self, current_handles):
¡¡¡¡        self.current_handles = current_handles
¡¡¡¡    def __call__(self, driver):
¡¡¡¡        return len(driver.window_handles) > len(self.current_handles)
¡¡¡¡±¾ÎÄÄÚÈݲ»ÓÃÓÚÉÌҵĿµÄ£¬ÈçÉ漰֪ʶ²úȨÎÊÌ⣬ÇëȨÀûÈËÁªÏµ51TestingС±à(021-64471599-8017)£¬ÎÒÃǽ«Á¢¼´´¦Àí
selenium ×Ô¶¯»¯²âÊÔ¿ò¼Ü
µ±Ç°Ã»ÓÐÆÀÂÛµã»÷·¢±íÆÀÂÛ

Ïà¹ØÔĶÁ