The difference between self and non-Self in a method of a class in Python

Source: Internet
Author: User

1. Recently, when executing a UI Automation script to log on to the Web site encountered a wonderful problem: After running the script Chromedriver will drive the browser to open the Web site execution script a series of interface operations, the browser automatically shuts down after the operation is finished(The point is here, I clearly did not call Webdriver's close () or quite () method, why it closed automatically, before writing a series of scripts did not appear this problem, is very puzzled)

2. In the online search of related issues, many say because the version of Google Browser and chromedriver version does not match, so I downloaded the latest version of the Google browser and corresponding to the Chromedriver version, found still not (during the period also has reduced browser version), I have tried many times to find out that this problem is still there. Calm down and think about it, I think it may not be the compatibility between the version of the problem, the location of the reason may be wrong, so in Baidu copied a script on the local run, found that the phenomenon of auto-closing the browser is not.

3. After carefully checking the script code, it is found that the method contains the WD variable before the difference between self and no self is caused

#coding: UTF8
From selenium import Webdriver
From config.params Import *

Class PullData ():
Path2= "E:/chromedriver.exe"
def Init(self,url):
Self.url=url

def openBrowser(self):            wd = webdriver.Chrome(self.path2)               wd.get(self.url)    wd.maximize_window()            (我的代码里面wd前面没有加self,后来加上self就没有那种现象了),如下:    self.wd = webdriver.Chrome(self.path2)           #如果wd变量前不加上self时,浏览器执行完操作后会自动关闭(如wd=webdriver.Chrome(self.path2)),    self.wd.get(self.url)    self.wd.maximize_window()    name=‘changle‘    print name

Pulld = PullData (URL)
Pulld.openbrowser ()

The difference between self and non-Self in a method of a class in Python

Related Article

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.