2016-11-09 190 views
0

我需要通過Selenium webdriver上傳文件。 但如果我使用類似:如何刪除標籤<input type =「file」>中的屬性「

driver.findElement(By.xpath("//input[@type='file']")).sendKeys(file.getAbsolutePath()); 

然後我得到了一個錯誤:

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with 
Command duration or timeout: 128 milliseconds 
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700' 
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_91' 
Driver info: org.openqa.selenium.firefox.FirefoxDriver 

所以我想我需要刪除使用js的隱藏屬性,我發現這個代碼:

JavascriptExecutor jse = (JavascriptExecutor)driver; 
jse.executeScript("document.getElementsByTagName('*')[0].removeAttribute('hidden');"); 

但它不適合我。

這是我工作的代碼:

<label name="file" ng-model="file" ngf-accept="pattern" ngf-pattern="pattern" ngf-select="uploadSubmit($file)" ng-disabled="isUploadDisabled()" type="button" class="button button--large ng-pristine ng-untouched ng-valid ng-empty"> 
       <span translate="" class="button__text">Загрузить из файла .xls</span> 
       <input type="file" hidden="" ng-disabled="isUploadDisabled()"> 
      </label> 

回答

0

這應該工作:

試試:

console.log(document.getElementsByTagName("input")[0].removeAttribute('hidden')); 

OR

if (document.all !== undefined) 
{ 
    var allElements = document.all; 
} 
else 
{ 
    var allElements = document.getElementsByTagName("*"); 
} 

allElements[0].removeAttribute('hidden'); 
+0

我添加的代碼的一部分,你可以請我的情況格外 –

+0

我已經更新了我的回答,我已經檢查,然後張貼所以請關心這個 – Jigar7521

0

I W建議不要使用JS方法通過代碼刪除hidden屬性,因爲Selenium將用於模擬實際的用戶操作。

就像用戶點擊任何按鈕並且網站顯示那個div一樣。我建議你先觸發相同的組件,然後在顯示後使用隱藏的組件。

0

首先嚐試添加一些等待時間,以便元素可以加載到DOM中。 否則嘗試使用下面的代碼使元素可見。

WebElement element = yourWebDriverInstance.findElement(Locator); 

((JavascriptExecutor) yourWebDriverInstance).executeScript(arguments[0].style.height='auto'; arguments[0].style.visibility='visible';, element); 
相關問題