Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用selenium上传文件时出现问题_Java_Selenium Webdriver - Fatal编程技术网

Java 使用selenium上传文件时出现问题

Java 使用selenium上传文件时出现问题,java,selenium-webdriver,Java,Selenium Webdriver,我正在学习在一个网站上自动进行生产。我选择了一个自动化的网站。在使用上载图像按钮上载图像文件时,我遇到了一个问题,因为单击此按钮时似乎会转换为文本字段 我遵循的其他教程获取文本字段的id并使用sendkeys发送路径,因为它们有单独的文本字段和上载按钮键 以下是我尝试的代码: driver.get("http://logos.iti.gr/logos/"); driver.findElement(By.id("fileToUpload")).clear(); System.out.prin

我正在学习在一个网站上自动进行生产。我选择了一个自动化的网站。在使用上载图像按钮上载图像文件时,我遇到了一个问题,因为单击此按钮时似乎会转换为文本字段

我遵循的其他教程获取文本字段的id并使用sendkeys发送路径,因为它们有单独的文本字段和上载按钮键

以下是我尝试的代码:

 driver.get("http://logos.iti.gr/logos/");
 driver.findElement(By.id("fileToUpload")).clear();
 System.out.println("Cleared");              
 driver.findElement(By.id("fileToUpload")).sendKeys("/home/test.jpg");
我不知道是什么问题。它刚刚得到IPDL协议错误:处理程序返回错误代码

org.openqa.selenium.ElementNotInteractableException: Element <input id="fileToUpload" class="input_file" name="fileToUpload" type="file"> could not be scrolled into view
Build info: version: '3.141.5', revision: 'd54ebd709a', time: '2018-11-06T11:42:16'
org.openqa.selenium.elementNotInteractiableException:元素无法滚动到视图中
构建信息:版本:“3.141.5”,修订版:“d54ebd709a”,时间:“2018-11-06T11:42:16”

尝试更新您的客户端和Firefox。希望这能解决这个问题

我从来没有遇到过文件上传的任何问题,直到网页在单击图像上传时命令对话框。我正在使用最新版本的ChromeDriver和Chrome


如果可能的话,您也可以切换到ChromeDriver,因为根据我的经验,我发现这两个人可以无缝地协同工作!:)

您会得到
elementnotinteractivatableexception
-抛出该异常是为了指示尽管DOM中存在元素,但它不处于可以与之交互的状态。在您的例子中,发生这种情况是因为元素具有样式
display:none
。基本上,selenium(以及真实用户)不能与不可见元素交互。您需要使元素在第一个位置可见,然后继续

    driver.get("http://logos.iti.gr/logos/");
    WebElement el = driver.findElement(By.id("fileToUpload"));
    System.out.println("Making element visible");  
    ((JavascriptExecutor)driver).executeScript("arguments[0].style.display = 'block';", el);
    el.clear();
    System.out.println("Cleared");              
    el.sendKeys("/home/test.jpg");
顺便说一下,这里是默认的元素样式(您可以在浏览器开发工具中看到它)。注意
显示:无
。当您将值更改为
block
时,不要感到困惑,因为元素的宽度和高度都非常小,所以实际上在屏幕上看不到任何更改

.input_file {
    width: 0.1px;
    height: 0.1px;
    opacity: 0;
    overflow: hidden;
    position: absolute;
    z-index: -1;
    display: none;
}

你在用壁虎河吗?i、 e.测试正在启动Firefox浏览器。@TimothyT。是的,我正在使用geckodriver和FireFox浏览器你能帮我们使用你的FireFox版本吗?@MohamedAneesA我的FireFox版本是62.0.3(64位)。@MohamedAneesA我添加了更多关于错误的描述。我已经有最新版本的FireFox和selenium。我还没有解决它。我没有得到任何运气与铬太。我认为这与按钮如何变成文本框有关,但我不知道如何实现自动化。