The most powerful selenium Webdriver package in history.

Source: Internet
Author: User

Release this class, see test cases for usage.

1, based on the selenium two-time package, the overall code is very short and easy to understand, with two examples of custom methods for reference, easy to expand. 2, support unlimited instantiation of this class, still keep using the same browser window. 3, support the use of a custom method name, while the full support of the direct use of all the methods not defined in this class, but in the official class of API methods, such as direct support Driverwrapper (). Execute_script (script, *args) This notation. 4, the rest of the way to customize the name of the method can be under this class to write or inherit the class and then add other more methods
5, support a console log, accurate to the file name of the class name is on which line to print the log.

Tested to support Python2 and 3, support Chrom and Firefox, need to install selenium package and browser driver into the folder with environment variables. And then you can run it.
#Coding:utf-8ImportLoggingImportUnitTest fromSeleniumImportWebdriverclassCached_class_property (object):def __init__(Self, func): Self.func=funcdef __get__(self, obj, CLS):ifObj isNone:returnSelf value=self.func (obj) setattr (CLS, Self.func.__name__, value)returnvalueclassdriverwrapper ():"""1, based on the selenium two-time package, 2, support unlimited instantiation of this class, still maintain the use of the same browser window.    3. Support the use of custom method names, while direct support uses all methods not defined in this class, but has API methods in the official class, such as direct support for Driverwrapper (). Execute_script (script, *args). 4, the rest of the way to customize the name of the method can be under this class to write or inherit the class and then add other more methods"""    def __init__(self, driver_name):""":p Aram driver_name: Browser name number or letter"""Self.driver_name=driver_name @cached_class_propertydefdriver (self): Driver_var=NoneifSelf.driver_nameinch['Chrome', 1]: Driver_var=Webdriver. Chrome ()ifSelf.driver_nameinch['Firefox', 2]: Driver_var=Webdriver. Chrome ()returnDriver_var @cached_class_propertydeflogger (self): Logger_var= Logging.getlogger (self.__class__.__name__) Logger_var.setlevel (logging. DEBUG) Stream_handler=logging. Streamhandler () Stream_handler.setformatter (logging. Formatter ('% (asctime) s-% (name) s-% (filename) s-% (Lineno) d-% (levelname) s-% (message) s',"%y-%m-%d%h:%m:%s")) Logger_var.addhandler (Stream_handler)returnLogger_vardefopen (Self, URL): Self.driver.get (URL)defFind_element_by_css_selector (self, CSS_STR):#overriding the original method with a custom method, such as printing a paragraph firstSelf.logger.debug ('The CSS selector for the element you are looking for IS-'+css_str) self.driver.find_element_by_css_selector (CSS_STR)def __getattr__(Self, item):#want to add the other Webdriver operation method directly, not one by one and then write a method and then call the Driver property method, do not want to always make redundant code, you can do so. Python uses __getattribute__ first, can not find to call the __getsttr__ method, using this feature, to implement this add driver property into their own class inside        returngetattr (Self.driver, item)class_test (unittest. TestCase):defTest (self): Driver_wrapper= Driverwrapper (1) Driver_wrapper.open ('https://www.baidu.com')#Some people don't like to use GET, can call open whatDriver_wrapper.find_element_by_css_selector ('#kw')#when a method exists in a class, the method in its own class is preferred, so a log is printed each time a CSS selector is used to find an elementDRIVER_WRAPPER.FIND_ELEMENT_BY_ID ('kw')#when this method does not exist in the class, use the Chrome class methodDriver_wrapper2= Driverwrapper (1)#re-instantiate this class once, still can continue to use the previous browser, do not reload a browser windowDriver_wrapper2.open ('https://www.sina.com')
Driver_wrapper.logger.info (' run out, now want to close the browser ') Driver_wrapper2.driver.close ()#This can be done, but not as a dynamic add property, which is the method of directly using the driver property of the instance, the driver property is an instance of Chrome. if __name__=='__main__': Unittest.main ()

The most powerful selenium Webdriver package in history.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.