Python Selenium在输入字段不可见时上载图像/文件

2024-06-25 23:12:36 发布

您现在位置:Python中文网/ 问答频道 /正文

背景:

我管理一个Amazon商店,该类别不受Amazon批量上载工具的支持。该商店包含(一旦这个问题得到解决)+150个单独的产品,每个产品有6-9个预览图片,在6个欧盟网站(巨大的手动上传努力)。在

我决定使用Selenium和我的基本python技能来自动上传。在

问题:

除了图片上传外,一切都很好。我过去曾使用此代码将图像发送到输入字段:

driver.find_element_by_css_selector('input[type="file"]').send_keys('path/to/local/image')

将其修改为适合所讨论的图像上载页面:

^{pr2}$

返回一片红海:

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response raise exception_class(message, screen, stacktrace) ElementNotVisibleException: Message: element not visible (Session info: chrome=53.0.2785.116) (Driver info: chromedriver=2.23.409710 (0c4084804897ac45b5ff65a690ec6583b97225c0),platform=Mac OS X 10.11.6 x86_64)

也尝试过:

我已经读了一点,但是我对Javascript的知识是空白的,所以我盲目地尝试了一些Javascript片段,使元素首先可见,但没有效果。在

driver.execute_script('document.getElementById("main_image_url").style.visibility = "visible";')

我也读过一些解决方案,包括尝试用复杂的send_keys()组合导航实际的对话窗口,但听起来有点麻烦。在

我在Mac电脑上用Chrome驱动程序。在

有什么建议吗

页面的HTML元素:

Screenshot from page. actual code below

<div id="main_image_url-wrapper" class="a-row a-spacing-medium input-container"> <div data-attribute-name="main_image_url" data-image-index="0" class="a-row a-grid-vertical-align a-grid-center ilui-image-input-container image-not-selected hide-remove-button ilui-post-attribute"> <input id="main_image_url" name="main_image_url" value="" cssClass="image-source-of-truth" type="hidden" autocomplete="off" /> <div class="a-row"> <label for="main_image_url" class="label"> <span class="attribute-label-text">Main Image <span class="a-size-base a-color-tertiary a-text-italic a-text-normal"> (required) </span> </span> <span class="a-declarative" data-action="a-popover" data-a-popover="{&quot;name&quot;:&quot;attributeHelpmain_image_url&quot;,&quot;inlineContent&quot;:&quot;The URL where a main image of the product is located. It&#39;s important that this is supplied for all products.&quot;}"> <span class="help-icon"></span> </span> </label> </div> <div class="a-row ilui-image-wrapper ilui-visualization-element"> <span class="a-declarative" data-action="image-select-main_image_url" data-image-select-main_image_url="{}" id="image-container-declarative"> <div tabindex="0" class="ilui-image-select-container"> <div id="image-camera-icon" class="icon-container"> <i class="icon"></i> <ul class="a-nostyle a-vertical error-list image-error-list"></ul> </div> </div> </span> <img src="" class="ilui-img-thumbnail" /> <div class="image-loading-overlay a-hidden"> <div class="a-section upload-progress-meter image-progress-bar"> <div class="a-meter" aria-label="0%"> <div class="a-meter-bar" style="width: 0%;"></div> </div> <span class="percentage"></span> </div> </div> <span class="a-declarative" data-action="image-remove-button-main_image_url" data-image-remove-button-main_image_url="{}"> <div tabindex="0" class="remove-image-button"> <i class="a-icon a-icon-close"></i> </div> </span> </div> </div>

Tags: imagedividurlinputdatamaincontainer