有没有一种方法可以使用 Selenium 实现对 Magento 图片上传的拖放?
Is there a way to achieve drag and drop for Magento image upload using Selenium?
我正在尝试使用 python 和 selenium 在 magento 上自动上传产品,但是我 运行 在上传图片时遇到问题。
我尝试使用 id="fileupload"
定位输入标签
driver.find_element_by_id("fileupload").send_keys('C:\Users\PC\Desktop\Code\magento-bot\image1.png')
似乎可以,因为当我将鼠标指针放在上传区域时,会显示文件名,但提交后没有图像。
我也试过点击上传区域然后select文件上传:
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")
我们将不胜感激。
可能与以下内容重复
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)
另请参阅下面的线程
我的问题的临时解决方案是 AutoIt。
非常感谢@KunduK How to upload image with angular components using python selenium
我定位了图片上传区域的 xpath,然后 autoit 使用下面的代码完成了剩下的工作:
autoit.win_wait_active("File Upload",5)
if autoit.win_exists("File Upload"):
autoit.control_send("File Upload","Edit1",filepath+"{ENTER}")```
我正在尝试使用 python 和 selenium 在 magento 上自动上传产品,但是我 运行 在上传图片时遇到问题。
我尝试使用 id="fileupload"
driver.find_element_by_id("fileupload").send_keys('C:\Users\PC\Desktop\Code\magento-bot\image1.png')
似乎可以,因为当我将鼠标指针放在上传区域时,会显示文件名,但提交后没有图像。
我也试过点击上传区域然后select文件上传:
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")
我们将不胜感激。
可能与以下内容重复
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)
另请参阅下面的线程
我的问题的临时解决方案是 AutoIt。
非常感谢@KunduK How to upload image with angular components using python selenium
我定位了图片上传区域的 xpath,然后 autoit 使用下面的代码完成了剩下的工作:
autoit.win_wait_active("File Upload",5)
if autoit.win_exists("File Upload"):
autoit.control_send("File Upload","Edit1",filepath+"{ENTER}")```