没有输入元素的Python Selenium上传文件

时间:2019-07-20 05:57:58

标签: javascript python selenium file-upload

我在Python中使用Selenium尝试上传文件,但html元素不是输入元素。这是一个带有onclick的“ a”元素,该元素会调用javascript弹出浏览器窗口。

我尝试将send_keys用于“ a”按钮,但出现错误。我尝试更深入地研究“ a”元素,但都包含一个“ i”元素(一个图标)和一个“ span”元素,该元素显示“ Upload”。

我也尝试过使用win32com.client在“ a”元素上单击(),这将打开文件资源管理器并导航到我要上传的文件,然后发送“ {ENTER}”命令进行确认文件选择,但是没有用。

是否有一种方法可以将我想上传的文件注入到带有“ a”元素的onClick javascript中,我可以尝试一下,但是我只想对元素进行send_keys(filePATH)我不知道如何进行交互或在网页上找到javascript文件。

# I'm leaving out the code that is unnecessary 
import os
import time
import win32com.client
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select

chromedriver = "C:\\Users\\srichar133\\Downloads\\Daily Grind Auto Tool\\chromedriver.exe"

driver = webdriver.Chrome(chromedriver)


# normal webdriver way
uploadButton = driver.find_element_by_xpath('//*[@id="CRTaskDetailsAttachmentsInnerContainer"]/div[2]/div[1]/div[1]/div[2]/a')
uploadButton.send_keys("C://Users/srichar133/Downloads/Daily Grind Auto Tool/Standard CR MOP 5.0.docx")
time.sleep(2)

# win32com way
driver.find_element_by_xpath('//*[@id="CRTaskDetailsAttachmentsInnerContainer"]/div[2]/div[1]/div[1]/div[2]/a').click()
shell = win32com.client.Dispatch("WScript.Shell")
# I've already tried both ways of using backslashes in prior tests, the problem is elsewhere 
shell.Sendkeys("C:\\Users\\srichar133\\Downloads\\Daily Grind Auto Tool\\Standard CR MOP 5.0.docx")
time.sleep(3)
shell.Sendkeys("{ENTER}")
time.sleep(3)

'''
html code
<a class="btn btn-info svgfont" style="border-radius:24px; padding: 2px 30px;" onclick="Model.CRTaskDetails.BrowseFile()">
'''

当我执行send_keys命令时:selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

当我执行win32com方式时,脚本会忽略Sendkeys命令以查找文件目的地,然后经过长时间的等待,硒脚本会转到列表的下一个单击位置,并产生一个错误,表明无法单击该项目因为计算机仍在文件浏览器中,但未找到文件目标。

*其他编辑7/22/2019 8:34 AM *

我发现当用户单击按钮时激活的javascript。我可以通过javascript将附件发送到网站吗?

    self.LoadAttachment = function () {

        var attachData = null;
        if (Model.TaskDetails != undefined && Model.TaskDetails.taskDetails() != undefined && Model.TaskDetails.taskDetails()['MT_AttachmentData'] != undefined) {
            attachData = Model.TaskDetails.taskDetails()['MT_AttachmentData'];
        }

        if (attachData == undefined) {
            if (self.worklogsAttachmentPlugin != undefined) {
                var attachmentPluginObject = {
                    Model: self.Model,
                    ParentModel: 'Task',
                    TicketId: self.Wo_Id
                };
                self.worklogsAttachmentPlugin.pluginData = attachmentPluginObject;
                self.worklogsAttachmentPlugin.Load();
            }
        } else {
            if (self.worklogsAttachmentPlugin != undefined) {
                var attachmentPluginObject = {
                    Model: self.Model,
                    ParentModel: 'Task',
                    TicketId: self.Wo_Id
                };
                self.worklogsAttachmentPlugin.pluginData = attachmentPluginObject;
                self.worklogsAttachmentPlugin.SetAttachmentsPluginData(attachData);
            }
        }
    };

    self.OpenAttachments = function () {
        $('#' + self.Model + 'AttachmentsContainer').toggle('slow');
    };

    self.BrowseFile = function () {
        if (Object.keys(self.worklogsAttachmentPlugin.pluginData).length === 0) {
            self.LoadAttachment();
        }
        self.worklogsAttachmentPlugin.OpenFileBrowser();
        $('#' + self.Model + 'AttachmentsContainer').show('slow');
    };

    self.DownloadAttachment = function (attachmentName, type, attachmentId) {
        if (type == 3) {
            Helper.Utility.DownloadAttachment(attachmentName, self.Model);
        }
        else {
            Helper.Utility.DownloadDBDocument(attachmentName, self.Model, attachmentId);
        }
    };

0 个答案:

没有答案