gpt4 book ai didi

python - 使用 Selenium 通过 MacOS 窗口上传图像

转载 作者:太空宇宙 更新时间:2023-11-03 14:45:52 26 4
gpt4 key购买 nike

设置

我正在使用 Python 3.x 和 Selenium 在网上商店的后端工作。

我需要从我的电脑上传一张产品图片到平台的后台。


问题

图像上传按钮可通过 Selenium 单击,但单击会弹出 MacOS 文件选择器。

Selenium 不适用于 MacOS 文件选择器。我搜索了如何解决这个问题并找到了以下答案,

  1. https://stackoverflow.com/a/6129464/7326714
  2. http://www.seleniumstutorial.com/uploading-a-file-in-selenium-with-python/
  3. https://stackoverflow.com/a/10472542/7326714

然而,这些都不起作用。

按钮周围的 html 是,

<td class="control">


<div class="hide-input">
<input data-val="true" data-val-required="The Image field is required." id="FileName" name="FileName" type="text" value="File636578585362423712.png">
</div>

<div id="uploadifive-FileNameUpload" class="uploadifive-button" style="height: 18px; line-height: 18px; overflow: hidden; position: relative; text-align: center; width: 50px;">Upload<input id="FileNameUpload" type="file" name="FileNameUpload" data-editor="#FileName" data-url="http://also-inc.demohoster.com/upload/uploadfile" data-path="~/UserFiles/Products/Images/" data-maxsize="10240" data-extensions="*.jpg;*.jpeg;*.png;*.gif;*.bmp;" data-thumbnailwidth="128" data-thumbnailheight="128" data-thumbnailpath="/UserFiles/Products/Images/Preview/" data-uniquename="True" data-preview="/UserFiles/Products/Images/Preview/File636578585362423712.png" data-isnew="false" data-auth="2CDE59B99D5F034087CA006254189C31F4388BA02DDE1CD1752A2FCFDE6EB556406CAF9D82DE4E02AC4D7D9813E2CF8B2A1413EF7CE8CA22FDD9822130B4EC239F1BD305F8AA1E5F6E9EFD1CD64138B8A621A88A675A3A528B7DF2F0388C36C473CBAD080CF826A28A3464FB719D039690241E38" data-session="jx134u0kcaxfu4jz1darurvg" class="file-uploader hide-input" style="display: none;"><input type="file" style="font-size: 18px; opacity: 0; position: absolute; right: -3px; top: -3px; z-index: 999;"></div>
<div id="uploadifive-FileNameUpload-queue" class="uploadifive-queue"></div>
</td>

尝试

我试过点击按钮然后发送 key ,

browser.find_element_by_id('uploadifive-FileNameUpload').click()
browser.find_element_by_id('uploadifive-FileNameUpload').send_keys('path/to/MyImage.jpeg')

(还尝试了所有可能组合中的 id 'uploadifive-FileNameUpload-queue' 和 id 'FileName')

我试过不点击就发送,即 browser.find_element_by_id('uploadifive-FileNameUpload').send_keys('path/to/MyImage.jpeg')。再次为所有键。

无论我尝试什么,我都会收到 ElementNotInteractableException


这是我第一次尝试以这种方式上传图片,但我卡住了。

我该如何解决这个问题?

最佳答案

禁用文件选择器并调用 sendKeys<input type="file"> 上这是设计上唯一允许接收/保存文件的元素类型:

# disable the OS file picker
browser.execute_script("""
document.addEventListener('click', function(evt) {
if (evt.target.type === 'file')
evt.preventDefault();
}, true)
""")

# make an <input type="file"> available
browser.find_element_by_id('uploadifive-FileNameUpload')\
.click()

# assign the file to the <input type="file">
browser.find_element_by_css_selector('input[type=file]')\
.send_keys('path/to/MyImage.jpeg')

关于python - 使用 Selenium 通过 MacOS 窗口上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49540188/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com