硒警报自动单击IE驱动程序接受

时间:2020-03-20 01:46:41

标签: python-3.x selenium

在这种情况下: 页面上有一个列表 在它的下面有一个检查所有列表项的检查,还有一个div来接收所有列表, 单击后将弹出警报,单击接受后页面将关闭。

onclick=checkedalldoc();#check
onclick=responseclick();#div

我需要在IE中使用硒来自动处理这些 这是我的打包代码的basepage.py:

# 点击元素
    def click(self, *selector):
        el = self.find_element(*selector)
        try:
            el.click()
            logger.info('元素'%s' 已被点击.' % el.text)
        except NameError as e:
            logger.error('Failed to click the element with %s' % e)
#运行js脚本
    def run_script(self, js):
        try:
            self.driver.execute_script(js)
            logger.info('成功运行脚本,脚本内容为:%s.' % js)
        except BaseException:
            logger.error('js脚本运行错误.')
#切换至弹窗并点击确定
   def accept_alert(self):
        try:
            WebDriverWait(self.driver, 20).until(EC.alert_is_present())
            alert = self.driver.switch_to.alert
            alert.accept()
            logger.info('获得弹出框并选择确认.')
        except BaseException:
            logger.error('弹出框失败.')

这是逻辑功能

def receive_all_docs(self):
     self.click(*self.chkall_checkbox)
     self.click(*self.response_btn)
     self.accept_alert()

工作时,会提示以下错误,并且无法完成该功能:

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))

1 个答案:

答案 0 :(得分:0)

我会在这里尝试两件事
首先,语法:

as described in selenium-python docs,接受警报的方法是:

from selenium.webdriver.common.alert import Alert  
Alert(driver).accept()

第二次,您应该检查哪一行会产生异常。
当您尝试在警报打开时执行某项操作时,会发生UnexpectedAlertPresentException。警报已经打开时,是否有可能调用那些“单击”操作?