解决较高版本Chrome适配问题;不支持低于selenium4.1

This commit is contained in:
g1879 2022-05-13 18:19:57 +08:00
parent 34b76d9446
commit 6876d6a21c
10 changed files with 244 additions and 255 deletions

View File

@ -483,6 +483,9 @@ class DriverOptions(Options):
self.timeouts['pageLoad'] *= 1000
self.timeouts['script'] *= 1000
if '--no-sandbox' not in self._arguments:
self._arguments.append('--no-sandbox')
@property
def driver_path(self) -> str:
"""chromedriver文件路径"""

View File

@ -118,6 +118,11 @@ class Drission(object):
# -----------创建WebDriver对象-----------
self._driver = _create_driver(chrome_path, driver_path, self.driver_options)
# -----------解决接管新版浏览器不能定位到正确的标签页的问题-----------
active_tab = self._driver.window_handles[0]
if active_tab != self._driver.current_window_handle:
self._driver.switch_to.window(active_tab)
# 反反爬设置
try:
self._driver.execute_script('Object.defineProperty(navigator,"webdriver",{get:() => undefined,});')
@ -448,7 +453,7 @@ def _create_chrome(chrome_path: str, port: str, args: list, proxy: dict) -> tupl
# ----------创建浏览器进程----------
try:
debugger = Popen(f'{chrome_path} --remote-debugging-port={port} {args}', shell=False)
debugger = Popen(f'"{chrome_path}" --remote-debugging-port={port} {args}', shell=False)
if chrome_path == 'chrome.exe':
from .common import get_exe_path_from_port

View File

@ -764,10 +764,7 @@ class DriverElement(DrissionElement):
:param loc: 筛选条件可用selenium的(By, str)也可用本库定位语法
:return: DriverElement对象
"""
try:
from selenium.webdriver.support.relative_locator import RelativeBy
except ImportError:
raise ImportError('该方法只支持selenium4及以上版本。')
from selenium.webdriver.support.relative_locator import RelativeBy
if isinstance(loc, str):
loc = str_to_loc(loc)

View File

@ -334,15 +334,9 @@ class DriverPage(BasePage):
:param url: 新标签页跳转到的网址
:return: None
"""
try: # selenium4新增功能须配合新版浏览器
self.driver.switch_to.new_window('tab')
if url:
self.get(url)
except Exception:
self.to_tab(-1)
self.run_script(f'window.open("{url}");')
self.to_tab(-1)
self.driver.switch_to.new_window('tab')
if url:
self.get(url)
def close_tabs(self, num_or_handles: Union[int, str, list, tuple] = None) -> None:
"""关闭传入的标签页,默认关闭当前页。可传入多个 \n

View File

@ -201,7 +201,8 @@ running
# 设置线程数,只能在没有任务在运行的时候进行
page.download.roads = 20
# 大文件分块大小,默认 20MB page.downloadd.block_size = '50M'
# 大文件分块大小,默认 20MB
page.downloadd.block_size = '50M'
# 设置保存路径,设置后每个任务会使用这个路径,也可添加任务时单独设置
page.download.goal_path = r'D:\tmp'

View File

@ -53,8 +53,6 @@ html = ele.html
"""
```
## inner_html
此属性返回元素的`innerHTML`文本。
@ -68,8 +66,6 @@ Hello World!
"""
```
## tag
此属性返回元素的标签名。
@ -79,8 +75,6 @@ tag = ele.tag
# 返回div
```
## text
此属性返回元素内所有文本组合成的字符串。
@ -94,8 +88,6 @@ Hello World!
"""
```
## raw_text
此属性返回元素内原始文本。
@ -110,8 +102,6 @@ Hello World!
"""
```
## texts()
此方法返回元素内所有直接子节点的文本,包括元素和文本节点。 它有一个参数`text_node_only`,为`True`时则只获取只返回文本节点。这个方法适用于获取文本节点和元素节点混排的情况。
@ -376,4 +366,3 @@ displayed = ele.is_displayed()
## is_valid()
`DriverElement`一致。

View File

@ -149,11 +149,14 @@ print(page.timeouts)
此属性以列表形式返回当前浏览器所有标签页的 handle。
!> **注意:**
以下情况会导致获取到的顺序与视觉效果不一致1、自动化过程中手动点击标签页2、浏览器被接管时已打开一个以上标签页。
## current_tab_index
此属性返回当前标签页的序号。
!> **注意:** <br>自动化过程中若手动点击标签页,会使被点击标签页的 handle 排到首位,从而导致排序与视觉效果不一致
!> **注意:** <br>以下情况会导致获取到的排序号与视觉效果不一致1、自动化过程中手动点击标签页2、浏览器被接管时已打开一个以上标签页
## current_tab_handle
@ -176,4 +179,3 @@ print(page.timeouts)
- path: 下载文件夹路径,默认从配置信息读取
返回:下载中的文件列表

View File

@ -17,8 +17,6 @@
!> **注意:** <br>s 模式转 d 模式时,若浏览器当前网址域名和 s 模式不一样,必定会跳转。
参数:
- mode目标模式字符串`'s'``'d'`,默认转换到另一种
@ -299,7 +297,7 @@ page.to_frame(iframe)
返回None
!> **注意:** <br>自动化过程中若手动点击标签页,会使被点击标签页的 handle 排到首位,从而导致排序与视觉效果不一致
!> **注意:** <br>以下情况会导致获取到的排序号与视觉效果不一致1、自动化过程中手动点击标签页2、浏览器被接管时已打开一个以上标签页
```python
# 跳转到第一个标签页
@ -327,7 +325,7 @@ page.create_tab('http://www.baidu.com')
此方法用于关闭指定的标签页,标签页可以是序号或 handle 值,可关闭多个。默认关闭当前的。
!> **注意:** <br>当程序使用的是接管的浏览器,获取到的 handle 顺序和视觉效果不一致,不能按序号关闭
!> **注意:** <br>以下情况会导致获取到的排序号与视觉效果不一致不能按序号关闭1、自动化过程中手动点击标签页2、浏览器被接管时已打开一个以上标签页
参数:
@ -347,7 +345,7 @@ page.close_tabs((0, 2))
此方法用于关闭指定标签页以外的标签页,标签页可以是序号或 handle 值,可保留多个。默认保留当前的。
!> **注意:** <br>当程序使用的是接管的浏览器,获取到的 handle 顺序和视觉效果不一致,不能按序号关闭
!> **注意:** <br>以下情况会导致获取到的排序号与视觉效果不一致不能按序号关闭1、自动化过程中手动点击标签页2、浏览器被接管时已打开一个以上标签页
参数:

View File

@ -1,4 +1,4 @@
selenium
selenium>=4.1
requests
tldextract
lxml

View File

@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
setup(
name="DrissionPage",
version="2.5.9",
version="2.6.0",
author="g1879",
author_email="g1879@qq.com",
description="A module that integrates selenium and requests session, encapsulates common page operations.",
@ -18,7 +18,7 @@ setup(
include_package_data=True,
packages=find_packages(),
install_requires=[
"selenium",
"selenium>=4.1",
"lxml",
"tldextract",
"requests",