gpt4 book ai didi

java - 尽管我可以通过 xpath 选择元素,但在 Firefox 中获取 ElementNotVisibleException 异常

转载 作者:行者123 更新时间:2023-12-01 12:56:35 25 4
gpt4 key购买 nike

代码:

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

异常(exception):

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 找到所有元素。在包含 Alt=window.switchTo().alert(); 之前,代码工作正常,但将它们放入代码中后,我收到了这些异常。

最佳答案

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

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

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());

你完全可以将其更改为:

window.findElement(By.xpath("//input[@value='Ok']")).click();

请务必使用wait而不是Thread.sleep()

希望这能解决您的问题。

关于java - 尽管我可以通过 xpath 选择元素,但在 Firefox 中获取 ElementNotVisibleException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23835506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com