没有这样的元素异常

问题描述:

我得到错误,因为当为其中一个测试应用程序执行代码时找不到元素。我已经编写了使用css和xpath来定位元素的代码,但仍然遇到同样的问题。任何人都可以帮忙吗?没有这样的元素异常

代码:

public static WebDriver driver; 

    public static void setUp() { 

     System.setProperty("webdriver.ie.driver", "Resources\\IEDriverServer.exe"); 
     driver = new InternetExplorerDriver(); 

     //System.setProperty("webdriver.gecko.driver", "D:\\selenium\\geckodriver\\geckodriver.exe"); 

     driver.get("http://demo.actitime.com/"); 
     driver.manage().window().maximize(); 

     driver.findElement(By.id("username")).sendKeys("user"); 

     driver.findElement(By.name("pwd")).sendKeys("user"); 

     driver.findElement(By.cssSelector("#loginButton > div")).click(); 

     //Wait<WebDriver> wait=new WebDriverWait(driver, 30); 

     //wait.until(ExpectedConditions.presenceOfElementLocated(By.id("logoutLink"))); 

     //String parentWindow= driver.getWindowHandle(); 

     driver.findElement(By.cssSelector("div.popup_menu_icon.support_icon > div.popup_menu_arrow")).click(); 

//driver.findElement(By.xpath("id('topnav')/x:tbody/x:tr[1]/x:td[5]/x:table/x:tbody/x:tr/x:td[2]/x:div/x:table/x:tbody/x:tr[2]/x:td/x:div/x:div[2]/x:div/x:div[1]/x:div[2]")).click(); 


     driver.findElement(By.linkText("User Guide")).click(); 
    } 

    public static void tearDown() { 
     driver.quit(); 
    } 

    public static void main(String[] args) { 


     // TODO Auto-generated method stub 

     setUp(); 

     tearDown(); 


    } 

} 
+0

你可以添加错误信息到你的问题吗? – Casper

+0

线程“main”中的异常org.openqa.selenium.NoSuchElementException:无法用css选择器查​​找元素== div.popup_menu_icon.support_icon> div.popup_menu_arrow(警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时:834毫秒 有关此错误的文档,请访问:http://seleniumhq.org/exceptions/no_such_element.html –

+0

快速搜索dev。 http://demo.actitime.com/上的工具显示,此页面上没有“弹出式窗口”(类别和ID等) –

当你点击登录按钮需要浏览器几秒钟发送请求和返回/渲染响应页面,但在你的代码,你试图单击登录按钮(帮助按钮尚未显示)后单击右侧的帮助按钮,由于您仍然在登录页面上,因此无法在当前页面中找到您要查找的元素。

所以你需要等到登录后的页面被渲染,然后你可以选择并点击任何你想要的。

等待元素可以点击使用此代码:

WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds); 
wait.until(ExpectedConditions.elementToBeClickable(cssSelector("div.popup_menu_icon.support_icon > div.popup_menu_arrow"))); 

编辑:

看来你使用了错误的CSS选择器。试试这个:

cssSelector("div.popup_menu_button.popup_menu_button_support") 
+0

嘿,我试着按照你的信息..添加外部等待命令。但我仍然收到错误,找不到该元素。以下是错误信息。 –

+0

嘿,我试图根据你的信息..添加外部等待命令。但我仍然收到错误,找不到该元素。以下是线程“main”中的错误info.Exception org.openqa.selenium.TimeoutException:预期条件失败:等待元素可点击:By.cssSelector:div.popup_menu_icon.support_icon> div.popup_menu_arrow(尝试了50秒秒)与500毫秒的时间间隔) \t在 –

+0

问题得到解决,我可以选择用户指南。按照以下命令更改了css选择器。字符串parentWindow = driver.getWindowHandle(); 。 \t \t \t \t \t \t wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector( “div.popup_menu_icon.support_icon”)))点击(); –