mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
解决较高版本Chrome适配问题;不支持低于selenium4.1
This commit is contained in:
parent
34b76d9446
commit
6876d6a21c
@ -483,6 +483,9 @@ class DriverOptions(Options):
|
|||||||
self.timeouts['pageLoad'] *= 1000
|
self.timeouts['pageLoad'] *= 1000
|
||||||
self.timeouts['script'] *= 1000
|
self.timeouts['script'] *= 1000
|
||||||
|
|
||||||
|
if '--no-sandbox' not in self._arguments:
|
||||||
|
self._arguments.append('--no-sandbox')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def driver_path(self) -> str:
|
def driver_path(self) -> str:
|
||||||
"""chromedriver文件路径"""
|
"""chromedriver文件路径"""
|
||||||
|
@ -118,6 +118,11 @@ class Drission(object):
|
|||||||
# -----------创建WebDriver对象-----------
|
# -----------创建WebDriver对象-----------
|
||||||
self._driver = _create_driver(chrome_path, driver_path, self.driver_options)
|
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:
|
try:
|
||||||
self._driver.execute_script('Object.defineProperty(navigator,"webdriver",{get:() => undefined,});')
|
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:
|
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':
|
if chrome_path == 'chrome.exe':
|
||||||
from .common import get_exe_path_from_port
|
from .common import get_exe_path_from_port
|
||||||
|
@ -764,10 +764,7 @@ class DriverElement(DrissionElement):
|
|||||||
:param loc: 筛选条件,可用selenium的(By, str),也可用本库定位语法
|
:param loc: 筛选条件,可用selenium的(By, str),也可用本库定位语法
|
||||||
:return: DriverElement对象
|
:return: DriverElement对象
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
from selenium.webdriver.support.relative_locator import RelativeBy
|
from selenium.webdriver.support.relative_locator import RelativeBy
|
||||||
except ImportError:
|
|
||||||
raise ImportError('该方法只支持selenium4及以上版本。')
|
|
||||||
|
|
||||||
if isinstance(loc, str):
|
if isinstance(loc, str):
|
||||||
loc = str_to_loc(loc)
|
loc = str_to_loc(loc)
|
||||||
|
@ -334,16 +334,10 @@ class DriverPage(BasePage):
|
|||||||
:param url: 新标签页跳转到的网址
|
:param url: 新标签页跳转到的网址
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
try: # selenium4新增功能,须配合新版浏览器
|
|
||||||
self.driver.switch_to.new_window('tab')
|
self.driver.switch_to.new_window('tab')
|
||||||
if url:
|
if url:
|
||||||
self.get(url)
|
self.get(url)
|
||||||
|
|
||||||
except Exception:
|
|
||||||
self.to_tab(-1)
|
|
||||||
self.run_script(f'window.open("{url}");')
|
|
||||||
self.to_tab(-1)
|
|
||||||
|
|
||||||
def close_tabs(self, num_or_handles: Union[int, str, list, tuple] = None) -> None:
|
def close_tabs(self, num_or_handles: Union[int, str, list, tuple] = None) -> None:
|
||||||
"""关闭传入的标签页,默认关闭当前页。可传入多个 \n
|
"""关闭传入的标签页,默认关闭当前页。可传入多个 \n
|
||||||
注意:当程序使用的是接管的浏览器,获取到的 handle 顺序和视觉效果不一致,不能按序号关闭。 \n
|
注意:当程序使用的是接管的浏览器,获取到的 handle 顺序和视觉效果不一致,不能按序号关闭。 \n
|
||||||
|
@ -201,7 +201,8 @@ running
|
|||||||
# 设置线程数,只能在没有任务在运行的时候进行
|
# 设置线程数,只能在没有任务在运行的时候进行
|
||||||
page.download.roads = 20
|
page.download.roads = 20
|
||||||
|
|
||||||
# 大文件分块大小,默认 20MB
page.downloadd.block_size = '50M'
|
# 大文件分块大小,默认 20MB
|
||||||
|
page.downloadd.block_size = '50M'
|
||||||
|
|
||||||
# 设置保存路径,设置后每个任务会使用这个路径,也可添加任务时单独设置
|
# 设置保存路径,设置后每个任务会使用这个路径,也可添加任务时单独设置
|
||||||
page.download.goal_path = r'D:\tmp'
|
page.download.goal_path = r'D:\tmp'
|
||||||
|
@ -53,8 +53,6 @@ html = ele.html
|
|||||||
"""
|
"""
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## inner_html
|
## inner_html
|
||||||
|
|
||||||
此属性返回元素的`innerHTML`文本。
|
此属性返回元素的`innerHTML`文本。
|
||||||
@ -68,8 +66,6 @@ Hello World!
|
|||||||
"""
|
"""
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## tag
|
## tag
|
||||||
|
|
||||||
此属性返回元素的标签名。
|
此属性返回元素的标签名。
|
||||||
@ -79,8 +75,6 @@ tag = ele.tag
|
|||||||
# 返回:div
|
# 返回:div
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## text
|
## text
|
||||||
|
|
||||||
此属性返回元素内所有文本组合成的字符串。
|
此属性返回元素内所有文本组合成的字符串。
|
||||||
@ -94,8 +88,6 @@ Hello World!
|
|||||||
"""
|
"""
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## raw_text
|
## raw_text
|
||||||
|
|
||||||
此属性返回元素内原始文本。
|
此属性返回元素内原始文本。
|
||||||
@ -110,8 +102,6 @@ Hello World!
|
|||||||
"""
|
"""
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## texts()
|
## texts()
|
||||||
|
|
||||||
此方法返回元素内所有直接子节点的文本,包括元素和文本节点。 它有一个参数`text_node_only`,为`True`时则只获取只返回文本节点。这个方法适用于获取文本节点和元素节点混排的情况。
|
此方法返回元素内所有直接子节点的文本,包括元素和文本节点。 它有一个参数`text_node_only`,为`True`时则只获取只返回文本节点。这个方法适用于获取文本节点和元素节点混排的情况。
|
||||||
@ -376,4 +366,3 @@ displayed = ele.is_displayed()
|
|||||||
## is_valid()
|
## is_valid()
|
||||||
|
|
||||||
与`DriverElement`一致。
|
与`DriverElement`一致。
|
||||||
|
|
||||||
|
@ -149,11 +149,14 @@ print(page.timeouts)
|
|||||||
|
|
||||||
此属性以列表形式返回当前浏览器所有标签页的 handle。
|
此属性以列表形式返回当前浏览器所有标签页的 handle。
|
||||||
|
|
||||||
|
!> **注意:**
|
||||||
|
以下情况会导致获取到的顺序与视觉效果不一致:1、自动化过程中手动点击标签页;2、浏览器被接管时已打开一个以上标签页。
|
||||||
|
|
||||||
## current_tab_index
|
## current_tab_index
|
||||||
|
|
||||||
此属性返回当前标签页的序号。
|
此属性返回当前标签页的序号。
|
||||||
|
|
||||||
!> **注意:** <br>自动化过程中若手动点击标签页,会使被点击标签页的 handle 排到首位,从而导致排序与视觉效果不一致。
|
!> **注意:** <br>以下情况会导致获取到的排序号与视觉效果不一致:1、自动化过程中手动点击标签页;2、浏览器被接管时已打开一个以上标签页。
|
||||||
|
|
||||||
## current_tab_handle
|
## current_tab_handle
|
||||||
|
|
||||||
@ -176,4 +179,3 @@ print(page.timeouts)
|
|||||||
- path: 下载文件夹路径,默认从配置信息读取
|
- path: 下载文件夹路径,默认从配置信息读取
|
||||||
|
|
||||||
返回:下载中的文件列表
|
返回:下载中的文件列表
|
||||||
|
|
||||||
|
@ -17,8 +17,6 @@
|
|||||||
|
|
||||||
!> **注意:** <br>s 模式转 d 模式时,若浏览器当前网址域名和 s 模式不一样,必定会跳转。
|
!> **注意:** <br>s 模式转 d 模式时,若浏览器当前网址域名和 s 模式不一样,必定会跳转。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
参数:
|
参数:
|
||||||
|
|
||||||
- mode:目标模式字符串,`'s'`或`'d'`,默认转换到另一种
|
- mode:目标模式字符串,`'s'`或`'d'`,默认转换到另一种
|
||||||
@ -299,7 +297,7 @@ page.to_frame(iframe)
|
|||||||
|
|
||||||
返回:None
|
返回:None
|
||||||
|
|
||||||
!> **注意:** <br>自动化过程中若手动点击标签页,会使被点击标签页的 handle 排到首位,从而导致排序与视觉效果不一致。
|
!> **注意:** <br>以下情况会导致获取到的排序号与视觉效果不一致:1、自动化过程中手动点击标签页;2、浏览器被接管时已打开一个以上标签页。
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# 跳转到第一个标签页
|
# 跳转到第一个标签页
|
||||||
@ -327,7 +325,7 @@ page.create_tab('http://www.baidu.com')
|
|||||||
|
|
||||||
此方法用于关闭指定的标签页,标签页可以是序号或 handle 值,可关闭多个。默认关闭当前的。
|
此方法用于关闭指定的标签页,标签页可以是序号或 handle 值,可关闭多个。默认关闭当前的。
|
||||||
|
|
||||||
!> **注意:** <br>当程序使用的是接管的浏览器,获取到的 handle 顺序和视觉效果不一致,不能按序号关闭。
|
!> **注意:** <br>以下情况会导致获取到的排序号与视觉效果不一致,不能按序号关闭:1、自动化过程中手动点击标签页;2、浏览器被接管时已打开一个以上标签页。
|
||||||
|
|
||||||
参数:
|
参数:
|
||||||
|
|
||||||
@ -347,7 +345,7 @@ page.close_tabs((0, 2))
|
|||||||
|
|
||||||
此方法用于关闭指定标签页以外的标签页,标签页可以是序号或 handle 值,可保留多个。默认保留当前的。
|
此方法用于关闭指定标签页以外的标签页,标签页可以是序号或 handle 值,可保留多个。默认保留当前的。
|
||||||
|
|
||||||
!> **注意:** <br>当程序使用的是接管的浏览器,获取到的 handle 顺序和视觉效果不一致,不能按序号关闭。
|
!> **注意:** <br>以下情况会导致获取到的排序号与视觉效果不一致,不能按序号关闭:1、自动化过程中手动点击标签页;2、浏览器被接管时已打开一个以上标签页。
|
||||||
|
|
||||||
参数:
|
参数:
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
selenium
|
selenium>=4.1
|
||||||
requests
|
requests
|
||||||
tldextract
|
tldextract
|
||||||
lxml
|
lxml
|
||||||
|
4
setup.py
4
setup.py
@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="DrissionPage",
|
name="DrissionPage",
|
||||||
version="2.5.9",
|
version="2.6.0",
|
||||||
author="g1879",
|
author="g1879",
|
||||||
author_email="g1879@qq.com",
|
author_email="g1879@qq.com",
|
||||||
description="A module that integrates selenium and requests session, encapsulates common page operations.",
|
description="A module that integrates selenium and requests session, encapsulates common page operations.",
|
||||||
@ -18,7 +18,7 @@ setup(
|
|||||||
include_package_data=True,
|
include_package_data=True,
|
||||||
packages=find_packages(),
|
packages=find_packages(),
|
||||||
install_requires=[
|
install_requires=[
|
||||||
"selenium",
|
"selenium>=4.1",
|
||||||
"lxml",
|
"lxml",
|
||||||
"tldextract",
|
"tldextract",
|
||||||
"requests",
|
"requests",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user