selenium官网:https://seleniumhq.github.io/selenium/docs/api/py/
复用已有浏览器
针对mac下chrome浏览器debug启动配置:
https://blog.youkuaiyun.com/qq_15706111/article/details/112538391
https://blog.youkuaiyun.com/m0_46627257/article/details/123309944
在terminal里输入env
,看看$PATH如果没有Chrome的环境变量配置
如果没有,输入source ~/.bash_profile
更新一下,如果还是没有,那就是没配置Chrome的环境变量了,启动chrome浏览器debug启动配置:
vim ~/.bash_profile
export PATH=$PATH:/Applications/Google\ Chrome.app/Contents/MacOS
然后更新一下:source ~/.bash_profile
,再输入env
试试看,如果有了Chrome配置了,
尝试打开chrome:Google\ Chrome -remote-debugging-port=9222
打开Chrome后确保上面只有一个标签页,不然测试用例里的获取元素之类的都获取不到的
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ActionChains
from time import sleep
class Test_remote():
def setup(self):
option = Options()
option.debugger_address = "127.0.0.1:9222"
self.driver = webdriver.Chrome(options=option)
def test_move(self):
ele = self.driver.execute_script("return document.querySelector('.ant-avatar-icon img')")
action = ActionChains(self.driver)
action.move_to_element(ele)
action.perform()
self.driver.execute_script("document.querySelector('.bg-gray-700').children[2].click()")
sleep(5)
实现使用以前的cookies进行登录
# 实现使用以前的cookies进行登录
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ActionChains
from time import sleep
class Test_debugging:
def setup(self):
# option = Options()
# option.debugger_address = "127.0.0.1:9222"
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(5)
def test_debug(self):
# print(self.driver.current_window_handle,self.driver.window_handles)
# 因为每切换一次标签页,window_handles就会的顺序就会刷新一遍,所以获取需要调试标签页的id
# self.driver.switch_to.window('CDwindow-7F7B7597FFD93CE7D45FEE5FF570D8C5')
# avatar = self.driver.execute_script("return document.querySelector('.toolbar-btn-login a')")
# action = ActionChains(self.driver)
# action.move_to_element(avatar).perform()
# ele = self.driver.execute_script("return document.querySelectorAll('.csdn-border-bottom li')[1]")
# action.click(ele).perform()
# 当前页面操作登录后,获取到全部操作的cookie,然后copy下来,加入到driver中,实现打开新的标签页也能使用旧cookies
# print('cookies:', self.driver.get_cookies())
cookies = [{'domain': '.youkuaiyun.com', ... ....}]
self.driver.get("https://www.youkuaiyun.com")
for cookie in cookies:
self.driver.add_cookie(cookie)
self.driver.get("https://www.youkuaiyun.com")
sleep(8)
ps:pycharm格式化,option+command+L
将以前登录的cookies写入虚拟数据库
import shelve
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep
class Test_debugging:
def setup(self):
option = Options()
option.debugger_address = "127.0.0.1:9222"
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(5)
def test_debug(self):
self.driver.get('https://www.youkuaiyun.com')
db = shelve.open("cookies")
# db["cookie"] = self.driver.get_cookies()
cookies = db["cookie"]
for cookie in cookies:
if "expiry" in cookie.keys():
cookie.pop("expiry")
self.driver.add_cookie(cookie)
self.driver.get('https://www.youkuaiyun.com')
sleep(3)
db.close()