修复back()后退不准确的情况;修复'Secure-aa'和'Host-'开头的cookie不能设置的问题;ini文件删除chromedriver_path;修复wait.load_start()不能正确设置超时的问题;修复WebPage的get_cookies()方法不能获取所有域名的问题

This commit is contained in:
g1879 2023-07-27 15:23:40 +08:00
parent d0d33b5db8
commit d5da98084b
7 changed files with 35 additions and 56 deletions

View File

@ -542,9 +542,11 @@ class ChromiumBase(BasePage):
if ok: if ok:
try: try:
if single: if single:
return make_chromium_ele(self, node_id=nodeIds['nodeIds'][0]) r = make_chromium_ele(self, node_id=nodeIds['nodeIds'][0])
break
else: else:
return [make_chromium_ele(self, node_id=i) for i in nodeIds['nodeIds']] r = [make_chromium_ele(self, node_id=i) for i in nodeIds['nodeIds']]
break
except ElementLossError: except ElementLossError:
ok = False ok = False
@ -560,6 +562,12 @@ class ChromiumBase(BasePage):
sleep(.1) sleep(.1)
try:
self.run_cdp('DOM.discardSearchResults', searchId=search_result['searchId'])
except:
pass
return r
def refresh(self, ignore_cache=False): def refresh(self, ignore_cache=False):
"""刷新当前页面 """刷新当前页面
:param ignore_cache: 是否忽略缓存 :param ignore_cache: 是否忽略缓存
@ -595,14 +603,14 @@ class ChromiumBase(BasePage):
index = history['currentIndex'] index = history['currentIndex']
history = history['entries'] history = history['entries']
direction = 1 if steps > 0 else -1 direction = 1 if steps > 0 else -1
curr_url = history[index]['userTypedURL'] curr_url = history[index]['url']
nid = None nid = None
for num in range(abs(steps)): for num in range(abs(steps)):
for i in history[index::direction]: for i in history[index::direction]:
index += direction index += direction
if i['userTypedURL'] != curr_url: if i['url'] != curr_url:
nid = i['id'] nid = i['id']
curr_url = i['userTypedURL'] curr_url = i['url']
break break
if nid: if nid:
@ -1021,7 +1029,8 @@ class ChromiumBaseWaiter(object):
:return: 是否等待成功 :return: 是否等待成功
""" """
if timeout != 0: if timeout != 0:
timeout = self._driver.timeout if timeout in (None, True) else timeout if timeout is None or timeout is True:
timeout = self._driver.timeout
end_time = perf_counter() + timeout end_time = perf_counter() + timeout
while perf_counter() < end_time: while perf_counter() < end_time:
if self._driver.is_loading == start: if self._driver.is_loading == start:

View File

@ -292,9 +292,7 @@ class WebPageTab(SessionPage, ChromiumTab):
selenium_user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value'] selenium_user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value']
self.session.headers.update({"User-Agent": selenium_user_agent}) self.session.headers.update({"User-Agent": selenium_user_agent})
# set_session_cookies(self.session, self._get_driver_cookies(as_dict=True)) set_session_cookies(self.session, super(SessionPage, self).get_cookies())
# set_session_cookies(self.session, self._get_driver_cookies(all_domains=True))
set_session_cookies(self.session, self._get_driver_cookies())
def cookies_to_browser(self): def cookies_to_browser(self):
"""把session对象的cookies复制到浏览器""" """把session对象的cookies复制到浏览器"""
@ -315,22 +313,7 @@ class WebPageTab(SessionPage, ChromiumTab):
if self._mode == 's': if self._mode == 's':
return super().get_cookies(as_dict, all_domains, all_info) return super().get_cookies(as_dict, all_domains, all_info)
elif self._mode == 'd': elif self._mode == 'd':
return self._get_driver_cookies(as_dict, all_info) return super(SessionPage, self).get_cookies(as_dict, all_domains, all_info)
def _get_driver_cookies(self, as_dict=False, all_info=False):
"""获取浏览器cookies
:param as_dict: 是否以dict形式返回为True时all_info无效
:param all_info: 是否返回所有信息为False时只返回namevaluedomain
:return: cookies信息
"""
cookies = self.run_cdp('Network.getCookies')['cookies']
if as_dict:
return {cookie['name']: cookie['value'] for cookie in cookies}
elif all_info:
return cookies
else:
return [{'name': cookie['name'], 'value': cookie['value'], 'domain': cookie['domain']}
for cookie in cookies]
def _find_elements(self, loc_or_ele, timeout=None, single=True, relative=False, raise_err=None): def _find_elements(self, loc_or_ele, timeout=None, single=True, relative=False, raise_err=None):
"""返回页面中符合条件的元素、属性或节点文本,默认返回第一个 """返回页面中符合条件的元素、属性或节点文本,默认返回第一个

View File

@ -119,8 +119,6 @@ class WebPageTab(SessionPage, ChromiumTab):
def get_cookies(self, as_dict: bool = False, all_domains: bool = False, def get_cookies(self, as_dict: bool = False, all_domains: bool = False,
all_info: bool = False) -> Union[dict, list]: ... all_info: bool = False) -> Union[dict, list]: ...
def _get_driver_cookies(self, as_dict: bool = False, all_info: bool = False) -> dict: ...
# ----------------重写SessionPage的函数----------------------- # ----------------重写SessionPage的函数-----------------------
def post(self, def post(self,
url: str, url: str,

View File

@ -344,13 +344,21 @@ def set_browser_cookies(page, cookies):
if cookie['value'] is None: if cookie['value'] is None:
cookie['value'] = '' cookie['value'] = ''
if cookie.get('domain', None): if cookie['name'].startswith('__Secure-'):
try: cookie['secure'] = True
page.run_cdp_loaded('Network.setCookie', **cookie)
if is_cookie_in_driver(page, cookie): if cookie['name'].startswith('__Host-'):
continue cookie['path'] = '/'
except Exception: cookie['secure'] = True
pass
else:
if cookie.get('domain', None):
try:
page.run_cdp_loaded('Network.setCookie', **cookie)
if is_cookie_in_driver(page, cookie):
continue
except Exception:
pass
ex_url = extract(page._browser_url) ex_url = extract(page._browser_url)
d_list = ex_url.subdomain.split('.') d_list = ex_url.subdomain.split('.')

View File

@ -1,5 +1,4 @@
[paths] [paths]
chromedriver_path =
download_path = download_path =
[chrome_options] [chrome_options]

View File

@ -48,6 +48,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
self._download_set = None self._download_set = None
self._set = None self._set = None
self._screencast = None self._screencast = None
self._DownloadKit = None
self._set_start_options(driver_or_options, session_or_options) self._set_start_options(driver_or_options, session_or_options)
self._set_runtime_settings() self._set_runtime_settings()
@ -363,9 +364,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
selenium_user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value'] selenium_user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value']
self.session.headers.update({"User-Agent": selenium_user_agent}) self.session.headers.update({"User-Agent": selenium_user_agent})
# set_session_cookies(self.session, self._get_driver_cookies(as_dict=True)) set_session_cookies(self.session, super(SessionPage, self).get_cookies())
# set_session_cookies(self.session, self._get_driver_cookies(all_domains=True))
set_session_cookies(self.session, self._get_driver_cookies())
def cookies_to_browser(self): def cookies_to_browser(self):
"""把session对象的cookies复制到浏览器""" """把session对象的cookies复制到浏览器"""
@ -386,7 +385,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
if self._mode == 's': if self._mode == 's':
return super().get_cookies(as_dict, all_domains, all_info) return super().get_cookies(as_dict, all_domains, all_info)
elif self._mode == 'd': elif self._mode == 'd':
return self._get_driver_cookies(as_dict, all_info) return super(SessionPage, self).get_cookies(as_dict, all_domains, all_info)
def get_tab(self, tab_id=None): def get_tab(self, tab_id=None):
"""获取一个标签页对象 """获取一个标签页对象
@ -396,21 +395,6 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
tab_id = tab_id or self.tab_id tab_id = tab_id or self.tab_id
return WebPageTab(self, tab_id) return WebPageTab(self, tab_id)
def _get_driver_cookies(self, as_dict=False, all_info=False):
"""获取浏览器cookies
:param as_dict: 是否以dict形式返回为True时all_info无效
:param all_info: 是否返回所有信息
:return: cookies信息
"""
cookies = self.run_cdp('Network.getCookies')['cookies']
if as_dict:
return {cookie['name']: cookie['value'] for cookie in cookies}
elif all_info:
return cookies
else:
return [{'name': cookie['name'], 'value': cookie['value'], 'domain': cookie['domain']}
for cookie in cookies]
def close_driver(self): def close_driver(self):
"""关闭driver及浏览器""" """关闭driver及浏览器"""
if self._has_driver: if self._has_driver:

View File

@ -129,8 +129,6 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
def get_tab(self, tab_id: str = None) -> WebPageTab: ... def get_tab(self, tab_id: str = None) -> WebPageTab: ...
def _get_driver_cookies(self, as_dict: bool = False, all_info: bool = False) -> dict: ...
def close_driver(self) -> None: ... def close_driver(self) -> None: ...
def close_session(self) -> None: ... def close_session(self) -> None: ...