执行ActionChains(driver).click()与实际的鼠标单击有何不同? Python 3和WebDriver

时间:2018-08-14 17:17:00

标签: python selenium-webdriver

我正在尝试通过弹出窗口将文件上传到站点。我的代码可以插入文件路径,但是当我尝试单击“加载文件”按钮(属于网站的一部分)时,它会弹出ConnectionResetError

  • 直到该时间点我一直在运行脚本,然后手动单击“加载文件”并继续执行脚本,一切正常
  • 直到该时间点我一直在运行脚本,然后在iPython控制台中逐段执行命令,但仍然会引发错误

所以我的问题是,手动鼠标单击与action.click()有何不同?我必须相信这是该命令如何工作的基础。这是一个代码段,它会弹出错误:

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchWindowException, NoSuchElementException, UnexpectedAlertPresentException
from selenium.webdriver.common.action_chains import ActionChains

url = 'XXXXX'
driver = webdriver.Chrome(executable_path = r'[filepath]\chromedriver')
driver.get(url)

wait = WebDriverWait(driver, 5)

def attach_pdf(file_name):
    driver.find_element_by_name('[FILES TO ATTACH BUTTON]').click()
    driver.switch_to_window(driver.window_handles[-1])
    driver.find_element_by_id('[ATTACH FILE BUTTON]').click()
    driver.switch_to_window(driver.window_handles[-1])
    driver.find_element_by_name('file_to_upload').send_keys('[FILE PATH]' + file_name)
    driver.find_element_by_name('btLoad').click() ### <--- this is where the error occurs
    wait.until(EC.invisibility_of_element_located((By.NAME, 'file_to_upload')))
    driver.switch_to_window(driver.window_handles[-1])
    wait.until(EC.element_to_be_clickable((By.NAME, 'buttonPDFForm')))
    driver.find_element_by_name('buttonPDFForm').click

错误:

Traceback (most recent call last):

  File "<ipython-input-79-9d408fc21dfa>", line 1, in <module>
    driver.find_element_by_name('btLoad').click()

  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)

  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)

  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    response = self.command_executor.execute(driver_command, params)

  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 472, in execute
    return self._request(command_info[0], url, body=data)

  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 496, in _request
    resp = self._conn.getresponse()

  File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1331, in getresponse
    response.begin()

  File "C:\ProgramData\Anaconda3\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()

  File "C:\ProgramData\Anaconda3\lib\http\client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")

  File "C:\ProgramData\Anaconda3\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)

ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

0 个答案:

没有答案