Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.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 在firefox中获取ElementNotVisibleException异常,尽管我可以通过xpath选择元素_Java_Exception_Xpath_Exception Handling_Selenium Webdriver - Fatal编程技术网

Java 在firefox中获取ElementNotVisibleException异常,尽管我可以通过xpath选择元素

Java 在firefox中获取ElementNotVisibleException异常,尽管我可以通过xpath选择元素,java,exception,xpath,exception-handling,selenium-webdriver,Java,Exception,Xpath,Exception Handling,Selenium Webdriver,代码: 例外情况: package com.selenium; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Monster {

代码:

例外情况:

package com.selenium;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Monster {
    public static void main(String[] args)throws Exception{

        WebDriver window=new FirefoxDriver();
        try{
            window.get("https://my.monsterindia.com/login.html?src=http://my.monsterindia.com/my_monster.html&rand=5257");
            WebElement wb=window.findElement(By.id("BodyContent:txtUsername"));
            wb.sendKeys("shantanunandan8@gmail.com");
            window.findElement(By.id("BodyContent_txtPassword")).sendKeys("sha1nta1nu");
            Thread.sleep(4000);
            window.findElement(By.xpath("//input[@name='submit']")).click();
            Thread.sleep(4000);
            window.findElement(By.name("cancel")).click();
            window.findElement(By.className("flyout_close")).click();
            Thread.sleep(4000);
            window.findElement(By.xpath("//img[@src='http://media.monsterindia.com/monster_2012/trans.gif']")).click();
            Thread.sleep(4000);
            Alert alt=window.switchTo().alert();
            System.out.println("Massage from alert is "+alt.getText());
            alt.dismiss();
        }catch(Exception e){
            e.printStackTrace();
            Thread.sleep(4000);
            window.findElement(By.xpath("//input[@value='Cancel']")).click();
            System.out.println("Closing");
            window.close();
        }//catch close
        finally{
            window.close();
        }//finally close
    }//main close

}//class close
org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互
命令持续时间或超时:47毫秒
构建信息:版本:“2.39.0”,修订版:“ff23eac”,时间:“2013-12-16 16:12:12”
系统信息:主机:'omega-d52b83806',ip:'10.0.0.5',os.name:'Windows XP',os.arch:'x86',os.version:'5.1',java.version:'1.7.0_45'
会话ID:6eebd4ca-d637-45fa-91e3-c5f18d1d3568
驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver
能力[{platform=XP,acceptSslCerts=true,javascriptEnabled=true,cssSelectorsEnabled=true,databaseEnabled=true,browserName=firefox,handlesAlerts=true,browserConnectionEnabled=true,webStorageEnabled=true,nativeEvents=false,rotatable=false,locationContextEnabled=true,applicationCacheEnabled=true,takesScreenshot=true,version=29.0.1}]
位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)
位于sun.reflect.NativeConstructorAccessorImpl.newInstance(未知源)
位于sun.reflect.delegatingConstructor或AccessorImpl.newInstance(未知源)
位于java.lang.reflect.Constructor.newInstance(未知源)
位于org.openqa.selenium.remote.ErrorHandler.CreateTrowable(ErrorHandler.java:193)
位于org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
位于org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
位于org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
在org.openqa.selenium.remote.RemoteWebElement.click上(RemoteWebElement.java:79)
位于com.selenium.Monster.main(Monster.java:24)
原因:org.openqa.selenium.remote.ErrorHandler$UnknownServerException:元素当前不可见,因此可能无法与之交互
构建信息:版本:“2.39.0”,修订版:“ff23eac”,时间:“2013-12-16 16:12:12”
系统信息:主机:'omega-d52b83806',ip:'10.0.0.5',os.name:'Windows XP',os.arch:'x86',os.version:'5.1',java.version:'1.7.0_45'
驱动程序信息:驱动程序。版本:未知
at.fxdriver.premissions.visible(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/Anonymous580265852301620722网络驱动程序配置文件/扩展/fxdriver@googlecode.com/组件/命令(处理器js:8179)
at.DelayedCommand.prototype.checkPremissions_(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/Anonymous580265852301620722网络驱动程序配置文件/扩展/fxdriver@googlecode.com/组件/命令(处理器js:10814)
at.DelayedCommand.prototype.executeInternal\uh(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/Anonymous580265852301620722网络驱动程序配置文件/扩展/fxdriver@googlecode.com/组件/命令(处理器js:10831)
at.DelayedCommand.prototype.executeInternal_(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/Anonymous580265852301620722网络驱动程序配置文件/扩展/fxdriver@googlecode.com/组件/命令(处理器js:10836)

在.DelayedCommand.prototype.execute/下一步是否会导致任何警报。如果是,则不会有任何问题。如果不是,则控制转到catch block(NoAlertPresentException),然后尝试单击不可见的取消按钮。只需交叉检查您的流和定位器

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 47 milliseconds
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12'
System info: host: 'omega-d52b83806', ip: '10.0.0.5', os.name: 'Windows XP', os.arch: 'x86',   os.version: '5.1', java.version: '1.7.0_45'
Session ID: 6eebd4ca-d637-45fa-91e3-c5f18d1d3568
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true,  cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true,  browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=false, rotatable=false,  locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=29.0.1}]
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:193)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:79)
at com.selenium.Monster.main(Monster.java:24)
 Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Element is not  currently visible and so may not be interacted with
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12'
System info: host: 'omega-d52b83806', ip: '10.0.0.5', os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.7.0_45'
Driver info: driver.version: unknown
at <anonymous class>.fxdriver.preconditions.visible(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous580265852301620722webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:8179)
at <anonymous class>.DelayedCommand.prototype.checkPreconditions_(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous580265852301620722webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10814)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous580265852301620722webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10831)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous580265852301620722webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10836)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous580265852301620722webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10778)
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not  currently visible and so may not be interacted with
Command duration or timeout: 31 milliseconds
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12'
System info: host: 'omega-d52b83806', ip: '10.0.0.5', os.name: 'Windows XP', os.arch: 'x86',  os.version: '5.1', java.version: '1.7.0_45'
Session ID: 6eebd4ca-d637-45fa-91e3-c5f18d1d3568
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=false, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=29.0.1}]
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:193)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:79)
at com.selenium.Monster.main(Monster.java:33)
 Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Element is not currently visible and so may not be interacted with
 Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12'
 System info: host: 'omega-d52b83806', ip: '10.0.0.5', os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.7.0_45'
 Driver info: driver.version: unknown
at <anonymous class>.fxdriver.preconditions.visible(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous580265852301620722webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:8179)
at <anonymous class>.DelayedCommand.prototype.checkPreconditions_(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous580265852301620722webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10814)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous580265852301620722webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10831)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous580265852301620722webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10836)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous580265852301620722webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10778)

您需要处理代码。需要进行以下更改才能使其正常工作:

首先:-用下面的代码段替换以下内容,这解决了您的第一个异常。当您使用下面的
xpath
定位时,该页面有多个元素。它应该是您想要使用的唯一元素

window.findElement(By.xpath("//img[@src='http://media.monsterindia.com/monster_2012/trans.gif']")).click();
替换为:-

window.findElement(By.xpath("//img[@src='http://media.monsterindia.com/monster_2012/trans.gif']")).click();
第二:-删除
警报
。警报在第页中。请替换以下内容

window.findElement(By.xpath("//div[@id='main_resume']//div//a[6]")).click();
您完全可以将其更改为:

Alert alt=window.switchTo().alert();
System.out.println("Massage from alert is "+alt.getText());
请使用
wait
而不是
Thread.sleep()


希望这能解决您的问题。

Sir代码窗口。findElement(By.xpath(“//img[@src=”;导致删除按钮,当您单击该按钮时,会打开一个警告框,上面显示“您确定要删除此项目吗?”以及两个按钮“确定”和“取消”"。流程也是正确的。我还可以尝试其他什么吗?嗨,尼廷,问题是有一段时间我在登录页面的登录按钮上出现ElementNotVisible异常,因为我的代码可以输入电子邮件id和密码,但之后我会出现此异常。但是当我尝试运行相同的程序2-3次而代码没有任何更改时,它会出现异常工作了一段时间,但它再次给了我这个例外。为什么会发生这种情况?我在谷歌上搜索了它,但没有得到任何满意的答案。寻求帮助我可以通过luking@ur code看到这一点,但正如我说的,你需要改变如何定位你的
WebElement
。请使用
wait
来实现这一点。Nitin的窗口大小为m尝试?因为每当我在最大大小窗口中运行代码时,它都可以正常工作,但在小窗口中有时会运行,有时则不会。如果需要,我可以在运行代码时上载输出的完整视频,并且我还将Thread.sleep by window.manage().timeout().implicitlyWait(20,TimeUnit.SECONDS)替换为Thread.sleep by window.manage().timeout();正如你所建议的那样,我不认为这有什么关系。你能告诉我@这发生在哪一行吗?在这一行之后,我会得到一个异常窗口。findelelement(By.id(“BodyContent_txtPassword”))。sendKeys(“sha1nta1nu”);但现在我添加了window.manage().window().maximize()在我的代码中,从那时起,我不会得到ElementNotVisible异常。
window.findElement(By.xpath("//input[@value='Ok']")).click();