java - 上传图片 - 元素当前不可见

标签 java selenium selenium-webdriver

我正在尝试将图片上传到网站。

为此,我正在使用 Selenium 2.45.0。以下代码应上传照片:

public void uploadPhoto(String filePath) {
    filePath = filePath.replace("\\", "/");
    WebElement fileInput = driver.findElement(By.xpath("//div[@class='qq-upload-button']/input[@type='file']"));
    fileInput.sendKeys(filePath);
}

我正在使用以下浏览器:

public WebDriver driver = new FirefoxDriver();
public JavascriptExecutor jse = (JavascriptExecutor)driver;

但是,我得到以下异常:

Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 66 milliseconds
Build info: version: '2.45.0', revision: '5017cb8e7ca8e37638dc3091b2440b90a1d8686f', time: '2015-02-27 09:10:26'
System info: host: 'PC', ip: '197.368.536.12', os.name: 'Windows 8', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_45'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=38.0.1, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: fdfdf84b-4a99-4c47-bf88-b12518172011
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
    at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:89)
    at quotePictureCreator.util.SeleniumUtility.uploadPhoto(SeleniumUtility.java:62)
    at quotePictureCreator.main.RunApp.main(RunApp.java:50)
Caused by: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Build info: version: '2.45.0', revision: '5017cb8e7ca8e37638dc3091b2440b90a1d8686f', time: '2015-02-27 09:10:26'
System info: host: 'PC', ip: '192.168.56.1', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_45'
Driver info: driver.version: unknown
    at <anonymous class>.fxdriver.preconditions.visible(file:///C:/Users/AppData/Local/Temp/anonymous8332268915894898913webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:9587)
    at <anonymous class>.DelayedCommand.prototype.checkPreconditions_(file:///C:/Users/AppData/Local/Temp/anonymous8332268915894898913webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12257)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/AppData/Local/Temp/anonymous8332268915894898913webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12274)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/AppData/Local/Temp/anonymous8332268915894898913webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12279)
    at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/AppData/Local/Temp/anonymous8332268915894898913webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12221)

有什么建议我做错了吗? 感谢您的回复!

最佳答案

降级 firefox 可能会有帮助。到目前为止,我将它与 firefox 34 和 chrome 43 一起使用,按预期工作。 但是,隐藏文件标签不适用于 IE。参见 thisthis

关于java - 上传图片 - 元素当前不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30383949/

相关文章:

java - 如何获取旧 ID 并将其插入新文本字段

python - 如何顺序增加for循环

java - 无法解析方法,为什么?

java - 创建变量来保存对象有时是多余的吗?

java - 在外部调用中使用 Spring @Async 的任何理由

java - 使用 servlet 无需重启的 Tomcat 编程部署

java - 用显式等待替换隐式等待(selenium webdriver 和 java)

flash - 如何使用 selenium webdriver 自动化基于 Flash 的页面

c# - 在 Selenium WebDriver C# 中等待元素的任何更好更可靠的方法?

java - 如果您使用单独的配置文件,则无法使用 Firefox geckodriver,它会给出错误 : java. lang.OutOfMemoryError:Java 堆空间