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