gpt4 book ai didi

python-3.x - 有没有办法使用Selenium实现Magento图像上传的拖放?

转载 作者:行者123 更新时间:2023-12-03 11:24:38 25 4
gpt4 key购买 nike

我正在尝试使用 python 和 selenium 在 magento 上自动上传产品,但是我在上传图像时遇到了问题。

我试图用 id="fileupload" 来定位输入标签

driver.find_element_by_id("fileupload").send_keys('C:\\Users\\PC\\Desktop\\Code\\magento-bot\\image1.png')  

这似乎有效,因为当我将鼠标指针放在上传区域时会显示文件名,但提交后没有图像。

我还尝试单击上传区域,然后通过执行以下操作选择要上传的文件:
uploadElement = driver.find_element_by_xpath('//html/body/div[2]/main/div[2]/div/div/div/div[2]/div[5]/div[2]/fieldset/div/div[2]/div[1]/div[1]/div[1]')
uploadElement.click()
driver.switch_to.active_element().send_keys(os.getcwd()+"\image1.png)

但我最终遇到了这个错误 'FirefoxWebElement' object is not callable最后,我尝试像这样模拟拖放:
element = os.getcwd()+"\image1.png"
target = bot.find_element_by_id('fileupload')
ActionChains(bot).drag_and_drop(element, target).perform

但我收到以下错误
AttributeError("move_to requires a WebElement")
drag and drop image

任何帮助将不胜感激。

最佳答案

可能重复以下

Python with Selenium: Drag and Drop from file system to webdriver?

JS_DROP_FILE = """
var target = arguments[0],
offsetX = arguments[1],
offsetY = arguments[2],
document = target.ownerDocument || document,
window = document.defaultView || window;

var input = document.createElement('INPUT');
input.type = 'file';
input.onchange = function () {
var rect = target.getBoundingClientRect(),
x = rect.left + (offsetX || (rect.width >> 1)),
y = rect.top + (offsetY || (rect.height >> 1)),
dataTransfer = { files: this.files };

['dragenter', 'dragover', 'drop'].forEach(function (name) {
var evt = document.createEvent('MouseEvent');
evt.initMouseEvent(name, !0, !0, window, 0, 0, 0, x, y, !1, !1, !1, !1, 0, null);
evt.dataTransfer = dataTransfer;
target.dispatchEvent(evt);
});

setTimeout(function () { document.body.removeChild(input); }, 25);
};
document.body.appendChild(input);
return input;
"""

def drag_and_drop_file(drop_target, path):
driver = drop_target.parent
file_input = driver.execute_script(JS_DROP_FILE, drop_target, 0, 0)
file_input.send_keys(path)

另请参阅下面的线程

Selenium: Drag and Drop from file system to WebDriver?

How to simulate HTML5 Drag and Drop in Selenium Webdriver?

关于python-3.x - 有没有办法使用Selenium实现Magento图像上传的拖放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56995792/

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