通过硒上传文件,但文件输入元素被隐藏 [英] Uploading a file through selenium but file input element is hidden

查看:85
本文介绍了通过硒上传文件,但文件输入元素被隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Selenium上传文件,但是我的输入文件元素被隐藏了.

I am trying to upload a file using Selenium but my input file element is hidden.

我隐藏的HTML是:

<input id="yui_3_9_0pr3_1_1361897421363_2239" type="file" style="visibility:hidden; width:0px; height: 0px;" multiple="" accept="">

,选择文件按钮的HTML为:

and the select file button HTML is:

<button id="yui_3_9_0pr3_1_1361897421363_2242" class="yui3-button" tabindex="0" aria-label="Select Files" role="button" type="button" style="width: 100%; height: 100%;">Select Files</button>

我尝试使用您建议的JavascriptExecutor进行相同的操作,但仍然会给出ElementNotVisible: Element is not currently visible异常.

I tried the same thing using JavascriptExecutor which you suggested but it still gives an exception ElementNotVisible: Element is not currently visible.

这是我的代码:

WebElement fileInput = driver.findElement(By.xpath(//@input[@type='file']));
System.out.println("h14");
String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";
((JavascriptExecutor) driver).executeScript(js, fileInput);
System.out.println("h15");
LocalFileDetector detector = new LocalFileDetector();
String path = "//Users//pdua//Desktop//images.jpeg";

// File f = detector.getLocalFile(path);
//((RemoteWebElement)fileInput).setFileDetector(detector);
System.out.println("h16");

//fileInput.sendKeys(f.getAbsolutePath());
fileInput.sendKeys(path);

隐藏的输入文件元素的XPath是//input[@type='file'].不确定是否正确!

The XPath of the hidden input file element is //input[@type='file']. Not sure if that is right or not!

推荐答案

如果元素不可见和/或不显示,则硒不会与元素交互.这可能是由于各种设置引起的:

Selenium will not interact with element if it is not visible and/or displayed. This could be caused by variety of the settings:

  • visibility=hidden;
  • display=none;
  • height=0width=0;
  • 在可显示坐标之外的位置(例如,left = -1)
  • visibility=hidden;
  • display=none;
  • height=0 or width=0;
  • position outside of displayable coordinate (e.g. left=-1)

在您的代码中,您显示heightwidth等于0,但仅重置height.尝试以下JS:

In your code you show height and width equal to 0, but only reset height. Try following JS:

String js = "arguments[0].style.height='1'; arguments[0].style.width='1'; "
            + "arguments[0].style.visibility='visible';";

此外,在浏览器中检查input[@type='file']元素,以检查是否应用了其他任何可影响可见性的样式或类.在我的情况下,有一个类应用于button包装input[@type='file']元素,并将其设置为display=none;.

Additionally, inspect the input[@type='file'] element in browser to check if there are any other styles or classes applied to it that can effect visibility. In my case, there was a class applied to button wrapping input[@type='file'] element, setting display=none;.

注意::更改元素可见性时,该测试正在修改该测试下的应用程序.不建议在测试中使用这种侵入性行为.

NOTE: When changing the element visibility, the test is modifying the application under the test. It is an intrusive behaviour not recommended for the tests.

更新:它显示在屏幕外部的元素(例如left=-1200)被报告为未在Selenium中显示,但不会阻止Selenium在其上执行sendKeys()方法.该方法没有返回类型,在这种情况下不会出现异常.

UPDATE: It appears element outside of the screen (e.g. left=-1200) is reported not displayed in Selenium, but it does not prevent Selenium to execute sendKeys() method on it. The method has no return type and does not through exception in this case.

这篇关于通过硒上传文件,但文件输入元素被隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆