首页 > 解决方案 > 关于硒获取下一页的问题

问题描述

有一个关于 webdriver.Ie() 的下一页的问题首先我需要登录:

import requests
from selenium import webdriver
driver = webdriver.Ie()
driver.implicitly_wait(10)
url="http://zboa3.sinopec.com/indishare/office.nsf/(frame)/tree"  #the url is LAN access only
driver.get(url)

# login
driver.find_element_by_name("Username").clear()
driver.find_element_by_name("Username").send_keys('myusername')
driver.find_element_by_name("Password").clear()
driver.find_element_by_name("Password").send_keys('password')
home_handle = driver.current_window_handle      
print(home_handle)                                    #display:0ba8173b-7373-4988-a8eb-2e4e03361eb3"
driver.find_elements_by_tag_name('img')[3].click()    #click the login button
home_handle1 = driver.current_window_handle
print('Login finish! '+driver.current_url)   #display:http://zboa3.sinopec.com/indishare/office.nsf/(frame)/tree
print(home_handle1)          #display:0ba8173b-7373-4988-a8eb-2e4e03361eb3

# click left nav a tab link
driver.switch_to.default_content()
driver.switch_to.frame("contents")
driver.find_element_by_link_text("部门收文").click()
home_handle2 = driver.current_window_handle
print("现在已切换至部门收文Tab "+driver.current_url) #display:http://zboa3.sinopec.com/indishare/office.nsf/(frame)/tree  
print(home_handle2)  #display:0ba8173b-7373-4988-a8eb-2e4e03361eb3

#click right button to a new page and get the table in it
driver.switch_to.default_content()
driver.switch_to.frame('main')
driver.find_element_by_id('smartdot_form_Button_1').click()
home_handle3 = driver.current_window_handle
print(home_handle3)
handles = driver.window_handles
print("现在已切换至收文页面 "+driver.current_url)
print(handles)
table = driver.find_element_by_xpath('//div[@id="viewDisplay"]/table')
rows = table.find_elements_by_tag_name('tr')
print(len(rows))

html有iframe,它获取了新页面,但是得到“urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(10054, '远程主机关闭了一个现有的连接。', None, 10054, None)) "error and ''' print(home_handle3) ''' can't run 问题信息:</p>

fae3457f-69a9-4ca9-beaa-75fb9800bbd3
Login finish! http://zboa3.sinopec.com/indishare/office.nsf/(frame)/tree
fae3457f-69a9-4ca9-beaa-75fb9800bbd3
现在已切换至部门收文tab http://zboa3.sinopec.com/indishare/office.nsf/(frame)/tree
fae3457f-69a9-4ca9-beaa-75fb9800bbd3
Traceback (most recent call last):
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 665, in urlopen
    httplib_response = self._make_request(
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 421, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\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 "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\A\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\new_ptvsd\no_wheels\ptvsd\__main__.py", line 45, in <module>
    cli.main()
  File "c:\Users\A\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\new_ptvsd\no_wheels\ptvsd/..\ptvsd\server\cli.py", line 361, in main
    run()
  File "c:\Users\A\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\new_ptvsd\no_wheels\ptvsd/..\ptvsd\server\cli.py", line 203, in run_file
    runpy.run_path(options.target, run_name="__main__")
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 263, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 96, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "e:\Dropbox\15-私人资料\code\python\自动化收文程序\main.py", line 56, in <module>
    driver.find_element_by_id('smartdot_form_Button_1').click()
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute     
    return self._parent.execute(command, params)
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 319, in execute
    response = self.command_executor.execute(driver_command, params)
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 374, in execute
    return self._request(command_info[0], url, body=data)
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 402, in _request
    resp = http.request(method, url, body=body, headers=headers)
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\request.py", line 79, in request
    return self.request_encode_body(
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\request.py", line 171, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\poolmanager.py", line 330, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 719, in urlopen
    retries = retries.increment(
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\retry.py", line 400, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\packages\six.py", line 734, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 665, in urlopen
    httplib_response = self._make_request(
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 421, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "C:\Users\A\AppData\Local\Programs\Python\Python38\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)) 

标签: pythonpython-3.xseleniumselenium-webdriver

解决方案


试试这个解决方案,因为元素似乎有点击的 Javascript 事件。它模拟javascripts点击事件

#Needed import
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep

element_to_click = driver.find_element_by_id('smartdot_form_Button_1')
ActionChains(driver).move_to_element(element_to_click).click().perform()

#Recommend to sleep after click, adjust time
sleep(3)

推荐阅读