简体   繁体   English

使用 selenium chromedriver 上传文件

[英]using selenium chromedriver to upload file

I'm working with selenium chromedriver.我正在使用 selenium chromedriver。 So far I have been able to log on, check a check box, navigate to another web page.到目前为止,我已经能够登录,选中一个复选框,导航到另一个 web 页面。 I've hit a brick wall though when I get to the upload file page.不过,当我进入上传文件页面时,我遇到了障碍。

The form has 4 or 5 buttons that will open up a dialog box to pick a file off of the local hard drive.该表单有 4 或 5 个按钮,它们将打开一个对话框以从本地硬盘驱动器中选择文件。 There is no input to put in directly.没有输入可以直接输入。 I think that when you click one of the buttons it executes a javascript function that choose a file.我认为当您单击其中一个按钮时,它会执行选择文件的 javascript function。 在此处输入图像描述 Please look at the source code of this page and see if you have any ideas how to accomplish this:请查看此页面的源代码,看看您是否有任何想法如何完成此操作:

^^^`enter code here<head><link rel="shortcut icon"  href="/favicon.ico"/><title>NYWB-ECF v10.5.2 NextGen Production Database</title>
<script type="text/javascript">document.cookie = "PRTYPE=aty; path=/;"</script> <script>var default_base_path = "/"; </script> <link rel="stylesheet" type="text/css" href="/css/default.css"><script type="text/javascript" src="/lib/core.js"></script><script type="text/javascript" src="/lib/autocomplete.js"></script><script type="text/javascript" src="/lib/DisableAJTALinks.js"></script><script type="text/javascript">if (top!=self) {top.location.replace(location.href);}</script><script>var default_base_path = "/"; </script></head><body BGCOLOR=ffffff TEXT=000000 onLoad='SetFocus()'>
                <div class="noprint">
                <div id="topmenu" class="yuimenubar"><div class="bd">
                <img id="cmecfLogo" class="cmecfLogo" src="/graphics/logo-cmecf-sm.png" alt="CM/ECF" title=""  />
                <ul class="first-of-type">
            
<li class="yuimenubaritem" ><a class="yuimenubaritemlabel" href='/cgi-bin/DisplayMenu.pl?BankruptcyEvents&id=492888'>Ban</u>kruptcy <div class='spritedownarrow'></div></a></li>
<li class="yuimenubaritem" ><a class="yuimenubaritemlabel" href='/cgi-bin/DisplayMenu.pl?AdversaryEvents&id=492888'>A</u>dversary <div class='spritedownarrow'></div></a></li>
<li class="yuimenubaritem" ><a class="yuimenubaritemlabel" href='/cgi-bin/iquery.pl'>Q</u>uery</a></li>
<li class="yuimenubaritem" ><a class="yuimenubaritemlabel" href='/cgi-bin/DisplayMenu.pl?Reports&id=492888'>R</u>eports <div class='spritedownarrow'></div></a></li>
<li class="yuimenubaritem" ><a class="yuimenubaritemlabel" href='/cgi-bin/DisplayMenu.pl?Utilities&id=492888'>U</u>tilities <div class='spritedownarrow'></div></a></li>
<li class="yuimenubaritem" ><a class="yuimenubaritemlabel" href='javascript: CMECF.MainMenu.searchPrompt();'>Searc</u>h</a></li>
<li class="yuimenubaritem" ><a class="yuimenubaritemlabel" onClick="CMECF.MainMenu.showHelpPage(''); return false">Help</a></li>
<li class="yuimenubaritem"><a class="yuimenubaritemlabel" href='/cgi-bin/login.pl?logout'>Log Out</a></li><li class='yuimenubaritem' id='placeholderForAlertsIcon'></li>
                </ul></div>
                <hr class="hrmenuseparator"></div></div>
                
            <script type="text/javascript">
callCreateMenu=function(){
                var fn = "CMECF.MainMenu.renderSimpleMenu";
                if(typeof CMECF.MainMenu.renderSimpleMenu == 'function') {
                    CMECF.MainMenu.renderSimpleMenu();
                }
                        }
if (navigator.appVersion.indexOf("MSIE")==-1){window.setTimeout( function(){ callCreateMenu(); }, 1);}else{CMECF.util.Event.addListener(window, "load",  callCreateMenu());}</script> <div id="cmecfMainContent"><input type="hidden" id="cmecfMainContentScroll" value="0"><SCRIPT LANGUAGE="JavaScript">
        var IsForm = false;
        var FirstField;
        function SetFocus() {
            if(IsForm) {
                if(FirstField) {
                    var ind = FirstField.indexOf('document.',0);
                    if(ind == 0)
                    {
                        eval(FirstField);
                    }
                    else
                    {
                        var Code = "document.forms[0]."+FirstField+".focus();";
                        eval(Code);
                    }
                } else {
                    var Cnt = 0;
                    while(document.forms[0].elements[Cnt] != null) {
                        try {
                            if(document.forms[0].elements[Cnt].type != "hidden" &&
                                    !document.forms[0].elements[Cnt].disabled &&
                                    !document.forms[0].elements[Cnt].readOnly) {
                                document.forms[0].elements[Cnt].focus();
                                break;
                            }
                        }
                        catch(e) {}
                        Cnt += 1;
                    }
                }
            }
            return(true);
        }
        </SCRIPT>

<div id="main-page-title" style="margin-bottom: .5em">
<font face="arial,helvetica" color="0000cc" size="4"><b><span id="main-page-title-content">Open New Bankruptcy Case</span></b></font>
</div> <FORM ENCTYPE='multipart/form-data' method=POST action="/cgi-bin/Dispatch.pl?127734820136425">
<!-ShowPage(CaseUpLoad.htm)-> 

<!-- RPM Packages: ao-bkecf-web-* -->
<!-- RPM Permissions: 444 -->
<!-- RPM Owner: nobody -->
<!-- RPM Group: nobody -->
<!-- RPM Flags: configure -->

 <script src="/lib/CommonLib.js" language="Javascript"></script>
<SCRIPT LANGUAGE="JavaScript">
function CheckCreditorMatrix(input,lower,upper,field) {
    var strng = "" + input;
    var len = strng.length;
    if (len < lower || len > upper) {
        var msg = "The List of Creditors is empty or does not exist";
        alert(msg);
        if (field) {
            field.focus();
            field.select();
        }
        return 0;
    } else if (len == 0) { return 2; }
    else return 1;
}
</script>

<table cellpadding="5"><tr>

<td><b>Case information</b><br/>
<input name="case_1" type="file" size="40" />
&nbsp;&nbsp;<font size = "-1">c:\Debtor.txt</font>
</td>

</tr><tr>

<td><b>Petition</b><br/>
<input name="file_1" type="file" size="40" />
&nbsp;&nbsp;<font size = "-1">c:\Petition.pdf</font>
</td>
</tr><tr>

<td><b>List of creditors</b><br/>
<input name = "creditorMatrixFilename" type="hidden" />
<input name="file_1_MTX" type="file" size="40" onChange="document.forms[0].creditorMatrixFilename.value = this.value"/>
&nbsp;&nbsp;<font size = "-1">c:\Creditor.txt</font>
</td>


<script>
var numEvents = 1;

// record the names so we know if the user tried to upload a file, even if it is invalid
function AssignHiddenValues() {
    document.forms[0].creditorMatrixFilename.value = document.forms[0].file_1_MTX.value;
    for (var i = 0; i <= numEvents; i++) {
        if(document.forms[0]["configured_file_"+i])
            document.forms[0]["configured_filename_"+i].value = document.forms[0]["configured_file_"+i].value;
    }
    return 1;
}
</script>







</tr><tr>
<td><b>Chapter 13 plan  (<em>chapter 13 only</em>)</b><br/>
<input name = "configured_filename_0" type="hidden" />
<input name="configured_file_0" type="file" size="40"
onChange="document.forms[0].configured_filename_0.value = this.value" />
&nbsp;&nbsp;<font size = "-1">c:\Plan.pdf</font>
</td>

</tr><tr>
<td><b>Certificate of credit counseling </b><br/>
<input name = "configured_filename_1" type="hidden" />
<input name="configured_file_1" type="file" size="40"
onChange="document.forms[0].configured_filename_1.value = this.value" />
&nbsp;&nbsp;<font size = "-1">c:\CCC.pdf</font>
</td>

</tr></table>
<script>numEvents = 2</script>

<SCRIPT LANGUAGE="JavaScript">
        document.cookie = "DPFFuncs='CaseUpLoadCollectCaseInfo-'; path=/; |";
        </SCRIPT>
        <SCRIPT LANGUAGE="JavaScript">
var timerId;
var BeenHere = 0;
IsForm  = true;
function empty(s) {
    var whitespace = " \    \
\
";
    if(s == null || s.length == 0) {
        return(true);
    }
    // Is s only whitespace characters?
    for (var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    return(true);
}
function ClearTimer() {
    BeenHere = 0;
    clearTimeout(timerId);
    return(true);
}
var FormId = 0;
function ProcessForm() {
    if(BeenHere == 1) {
        return(false);
    }
if( ! AssignHiddenValues() ) {return false}
if( ! CheckString(document.forms[0].case_1.value, 1, 1000, document.forms[0].case_1) ) {return false}
if( ! CheckString(document.forms[0].file_1.value, 1, 1000, document.forms[0].file_1) ) {return false}
if( ! CheckCreditorMatrix(document.forms[0].file_1_MTX.value, 1, 1000, document.forms[0].file_1_MTX) ) {return false}
BeenHere = 1;timerId=setTimeout("ClearTimer()",5000);document.forms[FormId].submit();return true }

function RunAfterClear() {
    
}
</SCRIPT>
<TABLE><TR> <TD><INPUT NAME="button1" Value='Next' TYPE= "button" ONCLICK="ProcessForm()" > <TD> <TD> <INPUT NAME="reset" TYPE="RESET" VALUE="Clear" onClick="setTimeout('RunAfterClear()',100)"></TD> </TR></TABLE></FORM></div></body></html>
}

You can click the choose file button, and use sendkeys to give your location of the file.您可以单击选择文件按钮,并使用 sendkeys 提供文件的位置。

WebElement upload = driver.findElement(By.linkText("Upload a Document:(.tex, .txt, .doc, .docx, .odt, .pdf, .rtf )")); WebElement upload = driver.findElement(By.linkText("上传文档:(.tex, .txt, .doc, .docx, .odt, .pdf, .rtf )")); upload.sendKeys("C:\path\File.txt"); upload.sendKeys("C:\path\File.txt");

Checkout this code.签出此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM