繁体   English   中英

如何删除标签中的“隐藏”属性<input type=“file”>

[英]How to delete attribute “hidden” in tag <input type=“file”>

我需要通过Selenium Webdriver上传文件。 但是,如果我使用类似:

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删除属性Hidden,我找到了以下代码:

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>

这应该工作:

试试吧 :

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

要么

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

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

我建议不要通过代码使用JS方法删除hidden属性,因为Selenium将用于模拟实际的用户操作。

就像用户单击任何按钮,然后网站显示该div 我建议您先触发相同的组件,然后在显示后使用隐藏的组件。

首先,尝试添加一些等待时间,以便可以将元素加载到DOM中。 否则,尝试使用下面的代码使该元素可见。

WebElement element = yourWebDriverInstance.findElement(Locator);

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

暂无
暂无

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

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