#coding=utf-8
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
import time
import os
import tensorflow
class SeleniumDriver:
’’‘
构造方法:
我实例这个对象直接运行,不调用任何方法会运行
’’'
def init(self,browser):
self.driver = self.open_browser(browser)
'''
二次封装打开浏览器
self:当前实例本身
'''
def open_browser(self,browser):
try:
if browser == 'chrome':
driver = webdriver.Chrome()
elif browser == 'firefox':
driver = webdriver.Firefox()
elif browser == 'ie':
driver = webdriver.Ie()
else:
driver = webdriver.Edge()
time.sleep(1)
self.driver = driver
return driver
except:
print("打开浏览器失败!")
return None
'''
二次封装打开浏览器
'''
def get_url(self,url):
if self.driver != None:
if 'http://' in url:
self.driver.get(url)
else:
print("您的URL有问题!")
else:
print("case失败!")
self.driver.quit()
'''
二次封装浏览器窗口常用操作
args[]:
1.value:
max 最大化
min 最小化
back 后退
forward 前进
refresh 刷新
2.int
'''
def handle_windows(self,*args):
value = len(args)
if value == 1:
if args[0] == 'max':
self.driver.maximize_window()
elif args[0] == 'min':
self.driver.minimize_window()
elif args[0] == 'back':
self.driver.back()
elif args[0] == 'forward':
self.driver.forward()
else:
self.driver.refresh()
elif value == 2:
self.driver.set_window_size(args[0],args[1])
else:
print("您传递的参数有问题!")
time.sleep(5)
self.driver.quit()
'''
判断title是否正确
'''
def assert_title(self,title_name=None):
if title_name != None:
get_title = EC.title_contains(title_name)
return get_title(self.driver)
'''
通过title判断页面打开的是否正确
'''
def open_url_is_true(self,url,title_name=None):
self.get_url(url)
return self.assert_title(title_name)
'''
关闭浏览器
'''
def close_driver(self):
self.driver.close()
selfnium_driver = SeleniumDriver(‘chrome’)
print(selfnium_driver.open_url_is_true(‘http://www.imooc.com’,‘程序员’))
执行结果:可以打开浏览器并且访问到慕课网站。
控制台输出:
Windows PowerShell
版权所有 © Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
PS D:\students> & D:/python/python.exe d:/students/open_browser.py
2020-12-29 17:15:17.348795: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library ‘cudart64_110.dll’; dlerror: cudart64_110.dll not found
2020-12-29 17:15:17.353644: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
DevTools listening on ws://127.0.0.1:55509/devtools/browser/29553294-e95e-4617-92d7-bdb859f7a6f9
Traceback (most recent call last):
File “D:\python\lib\site-packages\urllib3\connection.py”, line 170, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw
File “D:\python\lib\site-packages\urllib3\util\connection.py”, line 96, in create_connection
raise err
File “D:\python\lib\site-packages\urllib3\util\connection.py”, line 86, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “D:\python\lib\site-packages\urllib3\connectionpool.py”, line 706, in urlopen
chunked=chunked,
File “D:\python\lib\site-packages\urllib3\connectionpool.py”, line 394, in _make_request
conn.request(method, url, **httplib_request_kw)
File “D:\python\lib\site-packages\urllib3\connection.py”, line 234, in request
super(HTTPConnection, self).request(method, url, body=body, headers=headers)
File “D:\python\lib\http\client.py”, line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File “D:\python\lib\http\client.py”, line 1285, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File “D:\python\lib\http\client.py”, line 1234, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File “D:\python\lib\http\client.py”, line 1026, in _send_output
self.send(msg)
File “D:\python\lib\http\client.py”, line 964, in send
self.connect()
File “D:\python\lib\site-packages\urllib3\connection.py”, line 200, in connect
conn = self._new_conn()
File “D:\python\lib\site-packages\urllib3\connection.py”, line 182, in _new_conn
self, “Failed to establish a new connection: %s” % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x0000022539F653C8>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积
极拒绝,无法连接。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “d:/students/open_browser.py”, line 103, in
print(selfnium_driver.open_url_is_true(‘http://www.imooc.com’,‘程序员’))
File “d:/students/open_browser.py”, line 93, in open_url_is_true
return self.assert_title(title_name)
File “d:/students/open_browser.py”, line 86, in assert_title
return get_title(self.driver)
File “D:\python\lib\site-packages\selenium\webdriver\support\expected_conditions.py”, line 51, in call
return self.title in driver.title
File “D:\python\lib\site-packages\selenium\webdriver\remote\webdriver.py”, line 342, in title
resp = self.execute(Command.GET_TITLE)
File “D:\python\lib\site-packages\selenium\webdriver\remote\webdriver.py”, line 319, in execute
response = self.command_executor.execute(driver_command, params)
File “D:\python\lib\site-packages\selenium\webdriver\remote\remote_connection.py”, line 374, in execute
return self._request(command_info[0], url, body=data)
File “D:\python\lib\site-packages\selenium\webdriver\remote\remote_connection.py”, line 397, in _request
resp = self._conn.request(method, url, body=body, headers=headers)
File “D:\python\lib\site-packages\urllib3\request.py”, line 75, in request
method, url, fields=fields, headers=headers, **urlopen_kw
File “D:\python\lib\site-packages\urllib3\request.py”, line 96, in request_encode_url
return self.urlopen(method, url, **extra_kw)
File “D:\python\lib\site-packages\urllib3\poolmanager.py”, line 375, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File “D:\python\lib\site-packages\urllib3\connectionpool.py”, line 796, in urlopen
**response_kw
File “D:\python\lib\site-packages\urllib3\connectionpool.py”, line 796, in urlopen
**response_kw
File “D:\python\lib\site-packages\urllib3\connectionpool.py”, line 796, in urlopen
**response_kw
File “D:\python\lib\site-packages\urllib3\connectionpool.py”, line 756, in urlopen
method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
File “D:\python\lib\site-packages\urllib3\util\retry.py”, line 573, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host=‘127.0.0.1’, port=55503): Max retries exceeded with url: /session/05fb2855bd4421fa125e03c490000c98/title (Caused by NewConnectionError(’<urllib3.connection.HTTPConnection object at 0x0000022539F653C8>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。’,))
PS D:\students>