2016-10-13 2 views
4

Chromedriver를 사용하여 Selenium WebDriver를 통해 문서를 업로드해야합니다. 나는 모든 Action 클래스와 Javascript를 시도했지만 작동하지 않습니다. 버튼을 입력 필드로 사용하기 때문에 버튼이 작동하지 않기 때문에 작동하지 않는다고 가정합니다. 그것은 HTML은 다음과 같습니다이다 :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> 

I AutoIT 반입 및 Sikuli 구현 및 작동하지만, 그 솔루션의 문제는 내가 젠킨스를 통해 셀레늄 테스트를 실행할 때 그들이 일을 얻을 수있다 있습니다.

내 최신 시도는 다음과 같습니다 : 그것은 성공적를 통해 실행되지만 문서가 실제로 업로드되지 도착

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

.

답변

8

파일을 데스크톱에서 삭제하지 않는 한 브라우저는 <input> 요소가없는 파일을 업로드 할 수 없습니다. 코드로 파일을 업로드 할 수있는 보안 위반이 될 수 있습니다.

따라서 사용자가 링크를 클릭하면 <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"); 

참고 :이 사건을 처리하는

한 가지 방법은 click 이벤트를 침묵 링크를 클릭 한 다음하여 <input>에 파일을 설정하는 것입니다 .