Java 在webelement上获取NosTouchElementException(正在找到相同的webelement,但有时我会得到异常)

Java 在webelement上获取NosTouchElementException(正在找到相同的webelement,但有时我会得到异常),java,exception,selenium-webdriver,nosuchelementexception,Java,Exception,Selenium Webdriver,Nosuchelementexception,我创建了这个测试脚本来测试一个网站。问题是,有时我可以单击“编辑”按钮,但有时我会得到同一按钮的NoElementFoundException。我在论坛上搜索了相同类型的问题,并按照建议修改了代码,但没有一个对我有效。在所附图片中,我用红色圈出了按钮,我得到了例外 import java.util.NoSuchElementException; import java.util.concurrent.TimeUnit; import org.openqa.selenium.*; import o

我创建了这个测试脚本来测试一个网站。问题是,有时我可以单击“编辑”按钮,但有时我会得到同一按钮的NoElementFoundException。我在论坛上搜索了相同类型的问题,并按照建议修改了代码,但没有一个对我有效。在所附图片中,我用红色圈出了按钮,我得到了例外

import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class MonsterUpdateNewProfile {
    static WebDriver window=new FirefoxDriver();
    static WebElement wb;
    static String[] data;
    @Test(priority=2)
     static public boolean isAlertExist(){
         boolean alert=false;
         try{
            Alert alt= window.switchTo().alert();
            alert=true;
            System.out.println("Text from alert is "+alt.getText());
            alt.dismiss();
        }catch(Exception e){
            System.out.println("No Alert");
            e.printStackTrace();
        }
        return alert;
     }
     @Test
     static void dropdown() throws InterruptedException {
         wb=window.findElement(By.xpath("//div[@class='ns_menu_item_wrap ns_lt active']/a"));
         Actions mouse=new Actions(window);
         mouse.moveToElement(wb).perform();
         Thread.sleep(4000);
         Select select=new Select(wb);
         select.selectByIndex(1);
         }//dropdown
     @Test
     public static void monster() throws InterruptedException {
        try{
        //connecting to web site by sending address url
        window.get("https://my.monsterindia.com/login.html?src=http://my.monsterindia.com/my_monster.html&rand=5257");
        window.manage().window().maximize();
        window.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
        //finding username by using xpath
        wb=window.findElement(By.id("BodyContent:txtUsername"));
        //sending username
        wb.sendKeys("sshantanunandan@gmail.com");
        //finding and sending password
        window.findElement(By.id("BodyContent_txtPassword")).sendKeys("Ferrari2738");
        window.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
        //finding and clicking on login button
        window.findElement(By.name("submit")).click();
        //grtting the title of current page using getTitle() method
        System.out.println(window.getTitle());
        window.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
        window.findElement(By.name("cancel")).click();
        window.findElement(By.className("flyout_close")).click();
        //locating and clicking on update button which will open a new window
        window.findElement(By.xpath("//img[@src='http://media.monsterindia.com/monster_2012/btn_update2.gif']")).click();
        window.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
        //capturing the title of oldwindow or the window which we go after loging 
        //using getWindowHandle() method
        String oldwindow=window.getWindowHandle();
        //by using for statement getting the title of new pop upwindow
        //here we can use if or while statement as well using getWindowHandles() method
        for(String newwindow : window.getWindowHandles()){
        //swithching to the new pop up using window.switchTo().window(passing newwindow as argument)    
        window.switchTo().window(newwindow);}
        //getting title of new window using getTitle() method
        System.out.println("NewWindow Title"+ window.getTitle());
        window.findElement(By.xpath(".//*[@id='edit_resume_section3_open' and not(@disabled)]")).click();
        window.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
        System.out.println(window.findElement(By.xpath("//input[@name='title']")).getAttribute("value"));
        window.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        window.findElement(By.id("update")).click();
        window.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        window.close();
        //switching to oldwindow or main window
        //using switchTo().window(passing oldwindow url to it)
        //url which we captured in String oldwindow=window.getWindowHandle() method
        window.switchTo().window(oldwindow);
        //getting the output of old window
        System.out.println("OldWindow Title" + window.getTitle());
        //isAlertExist();
        //dropdown();
    }//try
        catch(NoSuchElementException e){
            window.findElement(By.className("flyout_close")).click();
            window.findElement(By.xpath("//a[@href='http://my.monsterindia.com/view_resume.html?resid=51706134&mode=edit']/img")).click();
            window.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
            window.findElement(By.id("edit_resume_section3_open")).click();
            window.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
            System.out.println(window.findElement(By.xpath("//input[@name='title']")).getAttribute("value"));
            window.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            window.findElement(By.id("edit_resume_section3_close_other")).click();
            window.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            //isAlertExist();
            dropdown();
        }//catch
        catch(Exception e){
            e.printStackTrace();
            System.out.println("Got Acception");
            window.close();
        }//catch
        finally{
            //dropdown();
            System.out.println("Finally Closing");
            window.close();
            window.quit();

        }//finally
  }//main
}//class

Exception got for this code-
   org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//input[@name='title']"}
 Command duration or timeout: 40.06 seconds
 For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
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: f4dce968-0425-4ba1-8a62-4ebdff91edc8
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.RemoteWebDriver.findElement(RemoteWebDriver.java:307)
at   org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:404)
at org.openqa.selenium.By$ByXPath.findElement(By.java:363)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
at MonsterUpdateNewProfile.monster(MonsterUpdateNewProfile.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at  org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to  locate element: {"method":"xpath","selector":"//input[@name='title']"}
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>.FirefoxDriver.prototype.findElementInternal_(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous591356915052066864webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:8860)
at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous591356915052066864webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:396)

import java.util.NoSuchElementException;
导入java.util.concurrent.TimeUnit;
导入org.openqa.selenium.*;
导入org.openqa.selenium.firefox.*;
导入org.openqa.selenium.interactions.Actions;
导入org.openqa.selenium.support.ui.Select;
导入org.testng.annotations.Test;
公共类MonsterUpdateNewProfile{
静态WebDriver窗口=新的FirefoxDriver();
静态web元素wb;
静态字符串[]数据;
@测试(优先级=2)
静态公共布尔isAlertExist(){
布尔警报=假;
试一试{
Alert alt=window.switchTo().Alert();
警报=真;
System.out.println(“来自警报的文本是”+alt.getText());
alt.Discrese();
}捕获(例外e){
System.out.println(“无警报”);
e、 printStackTrace();
}
返回警报;
}
@试验
静态void dropdown()引发InterruptedException{
wb=window.findElement(By.xpath(//div[@class='ns\u menu\u item\u wrap ns\u lt active']/a));
操作鼠标=新操作(窗口);
mouse.moveToElement(wb.perform();
睡眠(4000);
选择=新选择(wb);
select.selectByIndex(1);
}//下拉列表
@试验
public static void monster()抛出InterruptedException{
试一试{
//通过发送地址url连接到网站
窗口。获取(“https://my.monsterindia.com/login.html?src=http://my.monsterindia.com/my_monster.html&rand=5257");
window.manage().window().maximize();
window.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
//使用xpath查找用户名
wb=window.findElement(By.id(“BodyContent:txtUsername”);
//发送用户名
wb.sendKeys(“sshantanunandan@gmail.com");
//查找和发送密码
window.findElement(By.id(“BodyContent_txtPassword”)).sendKeys(“Ferrari2738”);
window.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
//查找并单击登录按钮
window.findElement(按.name(“提交”))。单击();
//使用getTitle()方法设置当前页面的标题
System.out.println(window.getTitle());
window.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
window.findElement(按.name(“取消”))。单击();
window.findElement(By.className(“弹出式按钮关闭”)。单击();
//找到并单击更新按钮,将打开一个新窗口
findElement(By.xpath(“//img[@src=”)http://media.monsterindia.com/monster_2012/btn_update2.gif”),单击();
window.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
//捕获oldwindow的标题或登录后的窗口
//使用getWindowHandle()方法
字符串oldwindow=window.getWindowHandle();
//通过使用for语句获取新弹出窗口的标题
//在这里,我们可以使用if或while语句,也可以使用getWindowHandles()方法
for(字符串newwindow:window.getWindowHandles()){
//使用window.switchTo().window切换到新弹出窗口(将newwindow作为参数传递)
window.switchTo().window(newwindow);}
//使用getTitle()方法获取新窗口的标题
System.out.println(“newwindowtitle”+window.getTitle());
window.findElement(按.xpath(“./*[@id='edit\u resume\u section3\u open'和not(@disabled)]”)。单击();
window.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
System.out.println(window.findElement(By.xpath(“//input[@name='title'])).getAttribute(“value”);
window.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
window.findElement(By.id(“更新”))。单击();
window.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
window.close();
//切换到旧窗口或主窗口
//使用switchTo().window(将oldwindow url传递给它)
//我们在字符串oldwindow=window.getWindowHandle()方法中捕获的url
window.switchTo().window(旧窗口);
//获取旧窗口的输出
System.out.println(“旧窗口标题”+window.getTitle());
//isAlertExist();
//下拉菜单();
}//试一试
捕获(无接触元素例外e){
window.findElement(By.className(“弹出式按钮关闭”)。单击();
window.findElement(By.xpath(“//a[@href=”http://my.monsterindia.com/view_resume.html?resid=51706134&mode=edit“]/img”)。单击();
window.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
window.findElement(By.id(“编辑”“恢复”“第3节”“打开”))。单击();
window.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
System.out.println(window.findElement(By.xpath(“//input[@name='title'])).getAttribute(“value”);
window.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
window.findElement(By.id(“编辑”“恢复”“第3节”“关闭”“其他”))。单击();
window.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
//isAlertExist();
下拉菜单();
}//抓住
捕获(例外e){
e、 printStackTrace();
System.out.println(“获得接受”);
window.close();
}//抓住
最后{
//下拉菜单();
System.out.println(“最终关闭”);
window.close();
window.quit();
....
WebDriverWait wait = new WebDriverWait(webDriver, 30)
window.findElement(By.id("...")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@name='title']")))
webDriver.findElement(By.xpath("//input[@name='title']"))
....
(new WebDriverWait(driver, 60)).until(new ExpectedCondition<WebElement>() {
  public WebElement apply(WebDriver d) {
       return d.findElement(By.xpath(".//*[@id='edit_resume_section3_open' and not(@disabled)]"));
    }
});