2016-10-13 14 views
4

Chromedriverを使用してSelenium WebDriverを使用して文書をアップロードする必要があります。私はすべてのアクションクラスとJavascriptのものを試しましたが、それらは動作しません。私は彼らが入力フィールドにボタンに依存しているので、彼らは動作しないと仮定していますが、私が扱っているアップロードボタンはありません。Selenium WebDriver - 非入力ボタンに文書をアップロード

<a id="Dialogs_Dialogs_lbAttachUpload" onclick="return ButtonVerifyEnabled(this, ShowFileSelect);" class="SkinButton sbBlue" onmouseover="ButtonHover(this,30);" onmouseout="ButtonLeave(this);" onmousedown="ButtonDown(this,30);" onmouseup="ButtonHover(this,30);" skinheight="30" style="color: white; width: 132px; height: 30px; line-height: 30px; background-position: 0px 0px;" title=""><div class="SkinButtonLeft" style="background-position: 0px 0px;"></div><div class="SkinButtonRight" style="background-position: -4px 0px;"></div>Upload documents</a> 

私はAutoItのとSikuliが実装され、作業持っているが、これらのソリューションに問題がジェンキンス経由Seleniumテストを実行しているとき、私は彼らが仕事を得ることができないです:それは、HTMLは次のようになります。

私の最新の試みは、次のようになります。

WebElement upload = SConfirmOrder.uploadDocuments_btn(driver); 
    Actions actions = new Actions(driver); 
    actions.moveToElement(upload); 
    actions.sendKeys("filepath\\Test PDF.pdf"); 

それは成功して実行されますが、何の文書が実際にアップロードされません取得します。

答えて

8

ファイルがデスクトップから削除されない限り、ブラウザは<input>要素のないファイルをアップロードすることはできません。コードでファイルをアップロードできるのはセキュリティの侵害です。

したがって、ユーザーがリンクをクリックすると、<input>が作成される可能性があります。

このケースを処理する1つの方法は、clickイベントを黙らリンクをクリックして、<input>にファイルを設定することです:あなたも<input>が作成されるのを待つ必要があるかもしれません

// disable the click event on an `<input>` file 
((JavascriptExecutor)driver).executeScript(
    "HTMLInputElement.prototype.click = function() {      " + 
    " if(this.type !== 'file') HTMLElement.prototype.click.call(this); " + 
    "};                 "); 

// trigger the upload 
driver.findElement(By.id("Dialogs_Dialogs_lbAttachUpload")) 
     .click(); 

// assign the file to the `<input>` 
driver.findElement(By.cssSelector("input[type=file]")) 
     .sendKeys("filepath\\Test PDF.pdf"); 

注意。

関連する問題