org.openqa.selenium.NoSuchElementException: no such element

限于喜欢 提交于 2019-12-03 14:51:10
Amith

As Yuvaraj HK has mentioned ,using implicit wait just once in your code would be enough.It'l implicitly wait for every element that you try to find in your code.

chrome.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

But try to keep implicit wait time as low as possible, because this might increase your code execution time..

In some cases the element might take more than 30 seconds to be visible, Explicit wait can be used in these kind of situations.

WebDriverWait some_element = new WebDriverWait(driver,100); 
some_element.until(ExpectedConditions.visibilityOfElementLocated(By.id("id_of_some_element")));
//do anything you want with some_element

I strongly suggest using cssSelectors over xpath. This article might help.
Even if xpath is used, try using shorter ones. Using an id is not the only way to reach an element. Its parent might have unique class names or other attributes, which you can use to create efficient xpaths or cssSelectors.

I guess your test fail sometimes due to the below statment

chrome.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

Just place the timeout code once in ur begninning of the test and remove all other instances.

 chrome.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

Right... so I managed to solve what was going on here. As I'm using IntelliJ, it seems I needed to tell it which order to call each method. So by adding @FixMethodOrder(MethodSorters.NAME_ASCENDING) and placing 'a' on my first method, then 'b' on my second method (I split the code I pasted in the question into methods since) and so on, it ran the methods in order with a wait on each method: try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. }

I had this same problem when trying to test IE7 on Windows Server 2003. I fixed it by removing Internet Explorer Enhanced Security Configuration. On Server 2003 you can fix it by doing the following:

  • Open up Control Panel
  • Open Add or Remove Programs
  • Open Add/Remove Windows Components (on the left sidebar)
  • Uncheck Internet Explorer Enhanced Security Configuration
  • Click Next
  • Follow through to the end
  • Restart Internet Explorer

If using Server 2008 or 2012 the steps will be different but the goal is the same.

did you open a new window? If yes, you need make driver to switch to the new window, following code is tested by me:

String currentWindow = driver.getWindowHandle();// get handle of current window
Set<String> handles = driver.getWindowHandles();// get handle of all windows
Iterator<String> it = handles.iterator();
while (it.hasNext()) {
if (currentWindow == it.next()) {
continue;
}
driver = driver.switchTo().window(it.next());// switch to new window

//business action
//xxxxxxx
}
driver.switchTo().window(currentWindow);//switch back to original window

Try use SeleniumWait library:

SeleniumWait.withDriver(driver).withTimeOut(15).forElementToClick(element);

It might be the browser issue. I had this problem and tried all sort of measures to fix it but didn't work until the Zoom percentage of the browser was fixed to 100%. this might sound a bit simple and funny but it worked for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!