添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
谦和的电池  ·  python 如何使用 selenium ...·  2 小时前    · 
冷冷的电梯  ·  Selenium – 哈呜.王·  昨天    · 
含蓄的枇杷  ·  文件版本_百度百科·  5 月前    · 
年轻有为的山羊  ·  Routinekarten·  5 月前    · 
没人理的砖头  ·  如何Prisma ...·  8 月前    · 
可爱的可乐  ·  迈向分割大一统 | ...·  12 月前    · 

如何利用selenium且不通过运行js脚本的方式传递url并在新窗口或者标签打开它。
利用window.open(‘url’),运行不了,并且提示

urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=51483): Max retries exceeded with url: /session/1f2311de-3086-4a15-be02-e3d6d6308a58/element (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001758103C880>: 
Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。'))

写了个小的demo测试,发现都存在这个问题

from selenium import webdriver
driver = webdriver.Ie()
driver.get('http://www.baidu.com')
newwindow = 'window.open("https://www.google.com")'
driver.execute_script(newwindow)
driver.switch_to_window(driver.window_handles[1])
driver.close()
driver.switch_to_window(driver.window_handles[0])

仍旧提示以下错误

Traceback (most recent call last):
  File "E:\Dropbox\17.Develop\5.Project\autozboa\env\lib\site-packages\urllib3\connectionpool.py", line 665, in urlopen      
    httplib_response = self._make_request(
  File "E:\Dropbox\17.Develop\5.Project\autozboa\env\lib\site-packages\urllib3\connectionpool.py", line 421, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "E:\Dropbox\17.Develop\5.Project\autozboa\env\lib\site-packages\urllib3\connectionpool.py", line 416, in _make_request
    httplib_response = conn.getresponse()
  File "c:\users\a\appdata\local\programs\python\python38\lib\http\client.py", line 1322, in getresponse
    response.begin()
  File "c:\users\a\appdata\local\programs\python\python38\lib\http\client.py", line 303, in begin       
    version, status, reason = self._read_status()
  File "c:\users\a\appdata\local\programs\python\python38\lib\http\client.py", line 264, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "c:\users\a\appdata\local\programs\python\python38\lib\socket.py", line 669, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "e:\Dropbox\17.Develop\5.Project\autozboa\run.py", line 22, in <module>
    manage()
  File "e:\Dropbox\17.Develop\5.Project\autozboa\run.py", line 14, in manage
    m.handle_doc()  # 处理单个文件
  File "e:\Dropbox\17.Develop\5.Project\autozboa\manage.py", line 67, in handle_doc
    docs_unid = docs.get_docs_unid()
  File "e:\Dropbox\17.Develop\5.Project\autozboa\zboa\pages\docs_page.py", line 38, in get_docs_unid
    for doc in self.find_elements(*self.docs_link):
  File "e:\Dropbox\17.Develop\5.Project\autozboa\zboa\framework\base_page.py", line 70, in find_elements
    elements = self.driver.find_elements(*selector)
  File "E:\Dropbox\17.Develop\5.Project\autozboa\env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1005, in find_elements
    return self.execute(Command.FIND_ELEMENTS, {
  File "E:\Dropbox\17.Develop\5.Project\autozboa\env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 319, in execute       
    response = self.command_executor.execute(driver_command, params)
  File "E:\Dropbox\17.Develop\5.Project\autozboa\env\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 374, in execute
    return self._request(command_info[0], url, body=data)
  File "E:\Dropbox\17.Develop\5.Project\autozboa\env\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 402, in _request
    resp = http.request(method, url, body=body, headers=headers)
  File "E:\Dropbox\17.Develop\5.Project\autozboa\env\lib\site-packages\urllib3\request.py", line 79, in request
    return self.request_encode_body(
  File "E:\Dropbox\17.Develop\5.Project\autozboa\env\lib\site-packages\urllib3\request.py", line 171, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "E:\Dropbox\17.Develop\5.Project\autozboa\env\lib\site-packages\urllib3\poolmanager.py", line 330, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "E:\Dropbox\17.Develop\5.Project\autozboa\env\lib\site-packages\urllib3\connectionpool.py", line 719, in urlopen
    retries = retries.increment(
  File "E:\Dropbox\17.Develop\5.Project\autozboa\env\lib\site-packages\urllib3\util\retry.py", line 400, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "E:\Dropbox\17.Develop\5.Project\autozboa\env\lib\site-packages\urllib3\packages\six.py", line 734, in reraise
    raise value.with_traceback(tb)
  File "E:\Dropbox\17.Develop\5.Project\autozboa\env\lib\site-packages\urllib3\connectionpool.py", line 665, in urlopen
    httplib_response = self._make_request(
  File "E:\Dropbox\17.Develop\5.Project\autozboa\env\lib\site-packages\urllib3\connectionpool.py", line 421, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "E:\Dropbox\17.Develop\5.Project\autozboa\env\lib\site-packages\urllib3\connectionpool.py", line 416, in _make_request
    httplib_response = conn.getresponse()
  File "c:\users\a\appdata\local\programs\python\python38\lib\http\client.py", line 1322, in getresponse
    response.begin()
  File "c:\users\a\appdata\local\programs\python\python38\lib\http\client.py", line 303, in begin
    version, status, reason = self._read_status()
  File "c:\users\a\appdata\local\programs\python\python38\lib\http\client.py", line 264, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "c:\users\a\appdata\local\programs\python\python38\lib\socket.py", line 669, in readinto
    return self._sock.recv_into(b)
urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None))

希望大家给出出主意,该如何解决?

按你的demo, 找一下问题

Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> driver = webdriver.Ie()
>>> print(driver.window_handles)
['ab749431-887e-4257-a6dd-344715ccc47d']
>>> print('Window Title', driver.title)
Window Title WebDriver

找到 webdriver 开的视窗标题为 WebDriver,
而且 window_handles = ['ab749431-887e-4257-a6dd-344715ccc47d']
注意该 handle 为 'ab749.....'

>>> driver.get('http://www.baidu.com')
>>> print(driver.window_handles)
['ab749431-887e-4257-a6dd-344715ccc47d']
>>> print('Window Title', driver.title)
Window Title 百度一下,你就知道

新开的视窗标题为 '百度一下,你就知道', 表示百度视窗已经打开了
注意该 handle 还是 'ab749.....', 表示是與 WebDriver 同一视窗

>>> newwindow = 'window.open("https://www.google.com")'
>>> driver.execute_script(newwindow)
>>> print(driver.window_handles)
['a4f878df-a053-47a1-8711-4206a0fc0a5a', 'ab749431-887e-4257-a6dd-344715ccc47d']
>>> print('Window Title', driver.title)
Window Title 百度一下,你就知道

在通过 JS 脚本的方式打开新窗口
此时视窗标题还是 '百度一下,你就知道', 代表雖然打幵了新视窗 Google, 但 WebDriver 还是指向百度视窗
另外 新的 handle 加了进来, 是加在index为0, 不是index为1
所以如果要切换到 google 视窗, 要 driver.switch_to.window(driver.window_handles[ 0 ])
而不是driver.window_handles[ 1 ]

>>> driver.switch_to.window(driver.window_handles[0])
>>> print(driver.window_handles)
['a4f878df-a053-47a1-8711-4206a0fc0a5a', 'ab749431-887e-4257-a6dd-344715ccc47d']
>>> print('Window Title', driver.title)
Window Title Google

现在标题为 'Google' 了, 表示视窗已经切换过来了

>>> driver.close()
>>> print(driver.window_handles)
['ab749431-887e-4257-a6dd-344715ccc47d']
>>> print('Window Title', driver.title)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Software\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 342, in title
    resp = self.execute(Command.GET_TITLE)
  File "D:\Software\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "D:\Software\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: Unable to get browser

视窗关闭, 所以 handles 中只剩百度, driver 所指向的视窗也不存在, 所以读取视窗标题会出错

>>> driver.switch_to.window(driver.window_handles[0])
>>> print(driver.window_handles)
['ab749431-887e-4257-a6dd-344715ccc47d']
>>> print('Window Title', driver.title)
Window Title 百度一下,你就知道

既然, google 视窗已关闭, handles 就只剩百度, 当然得用 driver.window_handles[0]

>>> driver.close()
>>> print(driver.window_handles)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Software\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 724, in window_handles
    return self.execute(Command.W3C_GET_WINDOW_HANDLES)['value']
  File "D:\Software\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "D:\Software\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSessionIdException: Message: session 31f6878d-e6eb-4324-ae4c-61f68a9bff98 does not exist
>>> print('Window Title', driver.title)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Software\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 342, in title
    resp = self.execute(Command.GET_TITLE)
  File "D:\Software\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "D:\Software\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSessionIdException: Message: session 31f6878d-e6eb-4324-ae4c-61f68a9bff98 does not exist

百度视窗也关闭了, 所以handles, title都会出错

driver.quit()

不用 driver 了, 最后再把 driver 也关闭 !

from selenium import webdriver
d = webdriver.Ie('IEDriverServer.exe')
print(d.window_handles)
d.get('http://www.baidu.com')
print(d.window_handles)
newwindow = 'window.open("https://www.google.com")'
d.execute_script(newwindow)
print(d.window_handles)
d.switch_to.window(d.window_handles[0])
print(d.window_handles)
print(d.title)
d.close()
d.quit()
['6f892e15-5a93-498e-82df-1a20517115cf']
['6f892e15-5a93-498e-82df-1a20517115cf']
Traceback (most recent call last):
  File "E:\Dropbox\17.Develop\2.Code\08.网络\selenium\env\lib\site-packages\urllib3\connectionpool.py", line 665, in urlopen
    httplib_response = self._make_request(
  File "E:\Dropbox\17.Develop\2.Code\08.网络\selenium\env\lib\site-packages\urllib3\connectionpool.py", line 421, in _make_request    
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "E:\Dropbox\17.Develop\2.Code\08.网络\selenium\env\lib\site-packages\urllib3\connectionpool.py", line 416, in _make_request
    httplib_response = conn.getresponse()
  File "c:\users\a\appdata\local\programs\python\python38\lib\http\client.py", line 1322, in getresponse
    response.begin()
  File "c:\users\a\appdata\local\programs\python\python38\lib\http\client.py", line 303, in begin
    version, status, reason = self._read_status()
  File "c:\users\a\appdata\local\programs\python\python38\lib\http\client.py", line 264, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "c:\users\a\appdata\local\programs\python\python38\lib\socket.py", line 669, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "e:\Dropbox\17.Develop\2.Code\08.网络\selenium\demo01.py", line 17, in <module>
    d.execute_script(newwindow)
  File "E:\Dropbox\17.Develop\2.Code\08.网络\selenium\env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 634, in execute_script
    return self.execute(command, {
  File "E:\Dropbox\17.Develop\2.Code\08.网络\selenium\env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 319, in execute
    response = self.command_executor.execute(driver_command, params)
  File "E:\Dropbox\17.Develop\2.Code\08.网络\selenium\env\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 374, in execute
    return self._request(command_info[0], url, body=data)
  File "E:\Dropbox\17.Develop\2.Code\08.网络\selenium\env\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 402, in _request
    resp = http.request(method, url, body=body, headers=headers)
  File "E:\Dropbox\17.Develop\2.Code\08.网络\selenium\env\lib\site-packages\urllib3\request.py", line 79, in request
    return self.request_encode_body(
  File "E:\Dropbox\17.Develop\2.Code\08.网络\selenium\env\lib\site-packages\urllib3\request.py", line 171, in request_encode_body     
    return self.urlopen(method, url, **extra_kw)
  File "E:\Dropbox\17.Develop\2.Code\08.网络\selenium\env\lib\site-packages\urllib3\poolmanager.py", line 330, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "E:\Dropbox\17.Develop\2.Code\08.网络\selenium\env\lib\site-packages\urllib3\connectionpool.py", line 719, in urlopen
    retries = retries.increment(
  File "E:\Dropbox\17.Develop\2.Code\08.网络\selenium\env\lib\site-packages\urllib3\util\retry.py", line 400, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "E:\Dropbox\17.Develop\2.Code\08.网络\selenium\env\lib\site-packages\urllib3\packages\six.py", line 734, in reraise
    raise value.with_traceback(tb)
  File "E:\Dropbox\17.Develop\2.Code\08.网络\selenium\env\lib\site-packages\urllib3\connectionpool.py", line 665, in urlopen
    httplib_response = self._make_request(
  File "E:\Dropbox\17.Develop\2.Code\08.网络\selenium\env\lib\site-packages\urllib3\connectionpool.py", line 421, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "E:\Dropbox\17.Develop\2.Code\08.网络\selenium\env\lib\site-packages\urllib3\connectionpool.py", line 416, in _make_request    
    httplib_response = conn.getresponse()
  File "c:\users\a\appdata\local\programs\python\python38\lib\http\client.py", line 1322, in getresponse
    response.begin()
  File "c:\users\a\appdata\local\programs\python\python38\lib\http\client.py", line 303, in begin
    version, status, reason = self._read_status()
  File "c:\users\a\appdata\local\programs\python\python38\lib\http\client.py", line 264, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "c:\users\a\appdata\local\programs\python\python38\lib\socket.py", line 669, in readinto
    return self._sock.recv_into(b)
urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None))

虽然完成了打开新标签页的动作,但是就中止了

d.execute_script (newwindow) 那表示还没 d.execute_script (newwindow), 就关闭连接了

问题不在 d.execute_script (newwindow)

这样问题不就缩小了, 目标就更明确 !

看了一下过程, 很可能是还没完全载入百度网页, 就作了下一步, 所以连接关闭了.

driver.get方法将导航到URL给定的页面. WebDriver将等待页面完全加载(即, 已触发"onload"事件), 然后再将控制权返回给测试或脚本. 值得注意的是,如果您的页面在加载时使用了很多AJAX,则WebDriver可能不知道何时已完全加载完毕.

也许你要等网页完全加载完毕,才能进行下一步.

不过我的执行环境没有这个问题,可能还是版本问题, 比如你的Python內建的 http包 socket.py 和我的就不一樣.

>>> urllib3.__version__
'1.25.8'
>>> sys.version
'3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)]'

当然啦,还可能有一个问题,是你的宽带不穩定....

d:\>ping -n 277504 www.baidu.com
Ping www.wshifen.com [103.235.46.39] (使用 32 位元組的資料):
回覆自 103.235.46.39: 位元組=32 時間=95ms TTL=46
回覆自 103.235.46.39: 位元組=32 時間=76ms TTL=46
回覆自 103.235.46.39: 位元組=32 時間=99ms TTL=46
回覆自 103.235.46.39: 位元組=32 時間=92ms TTL=46
回覆自 103.235.46.39: 位元組=32 時間=80ms TTL=46
回覆自 103.235.46.39: 位元組=32 時間=83ms TTL=46
回覆自 103.235.46.39: 位元組=32 時間=92ms TTL=46
回覆自 103.235.46.39: 位元組=32 時間=94ms TTL=46
回覆自 103.235.46.39: 位元組=32 時間=79ms TTL=46
回覆自 103.235.46.39: 位元組=32 時間=88ms TTL=46
回覆自 103.235.46.39: 位元組=32 時間=101ms TTL=46
回覆自 103.235.46.39: 位元組=32 時間=86ms TTL=46
103.235.46.39 的 Ping 統計資料:
    封包: 已傳送 = 12,已收到 = 12, 已遺失 = 0 (0% 遺失),
大約的來回時間 (毫秒):
    最小值 = 76ms,最大值 = 101ms,平均 = 88ms

以下步骤中间必须停下来, 确认browser已经作完动作.

from selenium import webdriver
d = webdriver.Ie('IEDriverServer.exe')

会出错吗?

print(d.window_handles)

会出错吗?

d.get('http://www.baidu.com')

打印了两个之后就出错了....问题不在 d.execute_script (newwindow)

有了新的进展, 然后呢? nothing ??

你可以自分析一下情况, 行吗? 再提供更多的讯息...... 很显然这与你Python的工作环境有关, 如果你没有自己捞出一些讯息, 别人是帮不了你的.

while not_OK():
    If condition1:    # you must go to find any condition different when you run your script.
        do_something_by_yourself_1()
    if conidtion2:
        do_something_by_yourself_2()
    if condition not in [condition1, condition2]
        get_help()

最笨的方法

  1. 移除 Python 3.8.1 (我也有些包不能用, 再回到3.7.6)
  2. 移除 Python 留下的痕迹, 比如 Python 目录, 安装的包, 安装包的缓存等等
  3. 重装 Python 3.7.6
  4. 重装 Selenium
  5. 重装 IE 11? 你的 IE 是什么版本?
  6. 我又跑了一次demo, 发现 handles 内的顺序不是一定的, 所以切换视窗不能简单的以索引来调用

    handles = driver.window_handles
    parent_handle = driver.current_window_handle
    index = 0 if handles[0] != parent_handle else 1
    driver.switch_to.window(driver.window_handles[index])