如何使用 Python Select 上传按钮并在 Selenium 中指定文档上传路径
How to Select Upload Button and Specify a Path for Document Upload in Selenium with Python
所以我必须 select 单击上一页上的编辑按钮后从新页面打开的页面上的上传按钮。当它打开一个新页面时,我必须 select 通过 select 上传要上传的文件 这是我尝试 select 该元素时得到的回溯:
Traceback (most recent call last):
File ".\skyward_collegeboard_TSI_import.py", line 143, in <module>
element = WebDriverWait(browser, 20).until(
File "C:\Python38\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
文件上传元素是这样的:
<input type="file" name="file1" id="file1" tabindex="55" size="76" det="true">
确认上传的按钮是这样的:
<a class="button" id="bUpload" tabindex="60" href="javascript:if (cbs("bUpload")) {Attach();}" role="button">Upload</a>
确认上传到服务器的保存按钮元素是这样的:
<a class="button" id="bSave" tabindex="500" href="javascript:if (cbs("bSave")) {checkSave("EditImport","simpthttp000.w","close");}" role="button" style=""><span class="hkey">S</span>ave</a>
您可以看到按钮的图片和我必须 select 下面文件的页面:
驱动它的主要代码构建在 python 和 selenium 包上,因为我们使用的程序没有任何 API,也不是使用现代 Web 应用程序堆栈构建的。我试过用XPATH
、NAME
、CSS_SELECTOR
和ID
点击这个按钮就可以select上传文件了。然而,我并没有取得太大的成功。
控制它的主要代码可以在这里找到:https://github.com/Richard-Barrett/ITDataServicesInfra/blob/master/Python/Skyward/skyward_collegeboard_TSI_import.py
控制此按钮点击的代码段:
# Upload Test File
# Choose File Button XPATH = //*[@id='file1']
element = WebDriverWait(browser, 20).until(
EC.element_to_be_clickable((By.NAME, "file1")))
element.click();
如前所述,我已经尝试了 NAME
、XPATH
和 ID
,其中 none 已经奏效。
我什至尝试为 select 这个按钮写一个单行代码,这样我就可以继续下一步上传,然后单击保存。
更新:尝试切换到 Window,因为总共有三个 windows。此外,我尝试了@supputuri 在下面提到的答案,并得到了以下回溯。
File ".\skyward_collegeboard_TSI_import.py", line 151, in <module>
element.send_keys("C:\Imports\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett")
File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT,
File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found : C:\Imports\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett
(Session info: chrome=80.0.3987.122)
现在处理该问题以切换到第三个 window 的代码是:
# Upload Test File
# Choose File Button XPATH = //*[@id='file1']
# Window Page Address that Opens = https://skyward-student.del-valle.k12.tx.us/scripts/wsisa.dll/WService=wsEAplus/simptedit000.w?isPopup=true
# Browser Switches to Window
WebDriverWait(browser,10).until(EC.number_of_windows_to_be(3))
browser.switch_to.window(browser.window_handles[-1])
#upload = browser.find_element_by_id("file1")
#upload.send_keys(r"C:\Users\richard.barrett\Downloads\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett")
element = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='file1']")))
element.send_keys("C:\Imports\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett")
这是我需要与 Selenium IDE 记录交互的特定元素:
{
"id": "2193d953-a105-45d0-8e6f-7fa910863cc2",
"comment": "",
"command": "click",
"target": "id=file1",
"targets": [
["id=file1", "id"],
["name=file1", "name"],
["css=#file1", "css:finder"],
["xpath=//input[@id='file1']", "xpath:attributes"],
["xpath=//div[@id='pageContentWrap']/table/tbody/tr/td/fieldset/table/tbody/tr[9]/td/fieldset/table/tbody/tr/td[2]/table/tbody/tr/td/input", "xpath:idRelative"],
["xpath=//td[2]/table/tbody/tr/td/input", "xpath:position"]
],
"value": ""
}, {
"id": "2f5cfebb-5e97-42da-97c6-2f7880fc2ec5",
"comment": "",
"command": "type",
"target": "id=file1",
"targets": [
["id=file1", "id"],
["name=file1", "name"],
["css=#file1", "css:finder"],
["xpath=//input[@id='file1']", "xpath:attributes"],
["xpath=//div[@id='pageContentWrap']/table/tbody/tr/td/fieldset/table/tbody/tr[9]/td/fieldset/table/tbody/tr/td[2]/table/tbody/tr/td/input", "xpath:idRelative"],
["xpath=//td[2]/table/tbody/tr/td/input", "xpath:position"]
],
"value": "C:\fakepath\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett.csv"
},
发送元素的文件路径。
element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.NAME, "file1")))
# don't click on the element, just send the path directly
element.send_keys("File/path/goes/here")
所以我必须 select 单击上一页上的编辑按钮后从新页面打开的页面上的上传按钮。当它打开一个新页面时,我必须 select 通过 select 上传要上传的文件 这是我尝试 select 该元素时得到的回溯:
Traceback (most recent call last):
File ".\skyward_collegeboard_TSI_import.py", line 143, in <module>
element = WebDriverWait(browser, 20).until(
File "C:\Python38\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
文件上传元素是这样的:
<input type="file" name="file1" id="file1" tabindex="55" size="76" det="true">
确认上传的按钮是这样的:
<a class="button" id="bUpload" tabindex="60" href="javascript:if (cbs("bUpload")) {Attach();}" role="button">Upload</a>
确认上传到服务器的保存按钮元素是这样的:
<a class="button" id="bSave" tabindex="500" href="javascript:if (cbs("bSave")) {checkSave("EditImport","simpthttp000.w","close");}" role="button" style=""><span class="hkey">S</span>ave</a>
您可以看到按钮的图片和我必须 select 下面文件的页面:
驱动它的主要代码构建在 python 和 selenium 包上,因为我们使用的程序没有任何 API,也不是使用现代 Web 应用程序堆栈构建的。我试过用XPATH
、NAME
、CSS_SELECTOR
和ID
点击这个按钮就可以select上传文件了。然而,我并没有取得太大的成功。
控制它的主要代码可以在这里找到:https://github.com/Richard-Barrett/ITDataServicesInfra/blob/master/Python/Skyward/skyward_collegeboard_TSI_import.py
控制此按钮点击的代码段:
# Upload Test File
# Choose File Button XPATH = //*[@id='file1']
element = WebDriverWait(browser, 20).until(
EC.element_to_be_clickable((By.NAME, "file1")))
element.click();
如前所述,我已经尝试了 NAME
、XPATH
和 ID
,其中 none 已经奏效。
我什至尝试为 select 这个按钮写一个单行代码,这样我就可以继续下一步上传,然后单击保存。
更新:尝试切换到 Window,因为总共有三个 windows。此外,我尝试了@supputuri 在下面提到的答案,并得到了以下回溯。
File ".\skyward_collegeboard_TSI_import.py", line 151, in <module>
element.send_keys("C:\Imports\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett")
File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT,
File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found : C:\Imports\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett
(Session info: chrome=80.0.3987.122)
现在处理该问题以切换到第三个 window 的代码是:
# Upload Test File
# Choose File Button XPATH = //*[@id='file1']
# Window Page Address that Opens = https://skyward-student.del-valle.k12.tx.us/scripts/wsisa.dll/WService=wsEAplus/simptedit000.w?isPopup=true
# Browser Switches to Window
WebDriverWait(browser,10).until(EC.number_of_windows_to_be(3))
browser.switch_to.window(browser.window_handles[-1])
#upload = browser.find_element_by_id("file1")
#upload.send_keys(r"C:\Users\richard.barrett\Downloads\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett")
element = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='file1']")))
element.send_keys("C:\Imports\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett")
这是我需要与 Selenium IDE 记录交互的特定元素:
{
"id": "2193d953-a105-45d0-8e6f-7fa910863cc2",
"comment": "",
"command": "click",
"target": "id=file1",
"targets": [
["id=file1", "id"],
["name=file1", "name"],
["css=#file1", "css:finder"],
["xpath=//input[@id='file1']", "xpath:attributes"],
["xpath=//div[@id='pageContentWrap']/table/tbody/tr/td/fieldset/table/tbody/tr[9]/td/fieldset/table/tbody/tr/td[2]/table/tbody/tr/td/input", "xpath:idRelative"],
["xpath=//td[2]/table/tbody/tr/td/input", "xpath:position"]
],
"value": ""
}, {
"id": "2f5cfebb-5e97-42da-97c6-2f7880fc2ec5",
"comment": "",
"command": "type",
"target": "id=file1",
"targets": [
["id=file1", "id"],
["name=file1", "name"],
["css=#file1", "css:finder"],
["xpath=//input[@id='file1']", "xpath:attributes"],
["xpath=//div[@id='pageContentWrap']/table/tbody/tr/td/fieldset/table/tbody/tr[9]/td/fieldset/table/tbody/tr/td[2]/table/tbody/tr/td/input", "xpath:idRelative"],
["xpath=//td[2]/table/tbody/tr/td/input", "xpath:position"]
],
"value": "C:\fakepath\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett.csv"
},
发送元素的文件路径。
element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.NAME, "file1")))
# don't click on the element, just send the path directly
element.send_keys("File/path/goes/here")