这是在python中使用selenium包的基本代码。
import time
from selenium import webdriver as wd
def main():
driver_c = wd.Chrome(executable_path='C:\\Web Drivers\\chromedriver\\chromedriver.exe')
driver_c.get('file:///D:/Website%20Code/Knight/Knight/index.html')
button = driver_c.find_element_by_xpath('//*[@id="hero"]/div/a[2]')
button.click()
time.sleep(4)
driver_c.close()
print(driver_c)
if __name__ == '__main__':
main()
上述自动化代码的结果是最后一个窗口关闭后得到两个Child Tab
aswellasParent Tab
该装置close()
称之为distroy浏览器。
但是如果同一main
函数命令置于全局范围内,它将不会引起争议,Child Tab
而会引起争议Parent Tab
。为什么结果会随范围而变化?
在两种情况下,对您的代码进行一些更改将导致相同的结果
import gc
gc.disable()
默认情况下python interpreter
,在main函数的末尾,由于,它会将您的driver_c对象散开gc.isenabled() is True
。通过禁用垃圾收集器,我们可以避免上述异常结果。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句