4

Am unable to click on element after executing a First test second test showing a org.openqa.selenium.NoSuchElementException . My code Here:

public class snapooh {
WebDriver driver; 

@BeforeTest
public void setup() throws Exception {
     driver =new FirefoxDriver();
     driver.manage().window().maximize();
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     driver.get(" http://abc/sptest/home ");
     System.out.println("Page Launched");
     }

@Test
public void login(){
    driver.findElement(By.className("loginBtn")).click();
    driver.findElement(By.id("emailaddress")).sendKeys("abc@abc.com");
    driver.findElement(By.id("loginpassword")).sendKeys("87654321");
    driver.findElement(By.id("loginEnter")).click();
  System.out.println("Login button pressed");

}
@Test
public void campaign() throws Exception {
    driver.findElement(By.id("usrUtils")).click();;

      driver.findElement(By.xpath(" .//*[@id='homeMainContainer']/div[1]/div/div/div[1]/div/ul/li[4]/a")).click();
      driver.findElement(By.className("addItem")).click();
      driver.findElement(By.id("campnNameInput")).sendKeys("Hello");
      driver.findElement(By.id("campnClientNameInput")).sendKeys("Hello");
      driver.findElement(By.id("campnDescInput")).sendKeys("Hello");
    //DateWidget Start date and end date

    driver.findElement(By.xpath("  .//*[@id='campnStartInput']")).click();
        WebElement   datepicker = driver.findElement(By.id("ui-datepicker-div"));  
        List<WebElement> rows=  datepicker.findElements(By.tagName("tr"));  
        List<WebElement> columns= datepicker.findElements(By.tagName("td"));  

                        for (WebElement cell: columns){  
                            //Select 20th Date   
                            if (cell.getText().equals("16")){  
                                cell.findElement(By.linkText("16")).click();   
                                    break;
                            }  
                                }   

                        driver.findElement(By.xpath("   .//*[@id='campnEndInput']")).click();
                        WebElement   datepicker1 = driver.findElement(By.id("ui-datepicker-div"));  
                        List<WebElement> rows1=  datepicker1.findElements(By.tagName("tr"));  
                        List<WebElement> columns1= datepicker1.findElements(By.tagName("td"));  
                        for (WebElement cell: columns1){  
                        //Select 20th Date   
                            if (cell.getText().equals("29")){  
                                cell.findElement(By.linkText("29")).click();   
                                    break;
                            }  
                                }   

    driver.findElement(By.id("createAndUpdateCampaign")).click();
    System.out.println("Campaign created");

}


    }

My Error Trace here :

 org.openqa.selenium.NoSuchElementException: Unable to locate element:   {"method":"id","selector":"usrUtils"}
 Command duration or timeout: 10.08 seconds
 For documentation on this error, please visit:  http://seleniumhq.org/exceptions/no_such_element.html
 Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:00:58'
 System info: host: 'Akshay-PC', ip: '192.168.56.1', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_80'
 *** Element info: {Using=id, value=usrUtils}
 Session ID: d2fcc233-1bb7-435c-ad51-c8958cba421c
 Driver info: org.openqa.selenium.firefox.FirefoxDriver
  • Selenium is not able to find the element. Either check the selector that you have used, or if the element appears after some time, then apply a wait and then do whatever you want to do with it. – demouser123 Jun 17 '16 at 7:19
4

You should wait for page load after click on button,so for that write below code :

WebElement element;
Webdriver driver;
WebDriverWait wait = new WebDriverWait(driver, 100);
element= wait.until(ExpectedConditions.elementToBeClickable(By.id("usrUtils")));
3

After you clicked loginenter button, some wait should be added to reload page. It will provide small delay which is helpful for SeleniumDriver to identify element. I would like to suggest you to add some condition to wait next element. Please try below code snippet

WebElement myDynamicElement = 
(new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("usrUTils")));
0

Why exception arise?

The reason behind the exception you have mentioned is because with your test script you are trying to locate an element that doesn't exist in the webpage you are trying to test.

How to resolve?

Well, along with the text you have provided, the console would also have given the line number in the test script where this exception occurred. I would go to that line in the test script and check what I've done wrong.

  • Assert with text or element to ensure you are on expected page/screen
  • I will suggest to use wait.untill(element.isVisible()) before Click() /Assert()
  • Have I provided a wrong value for searching the element?
  • Is the method wrong?
  • Am I missing something?

If answer to all these question is No and the element is indeed available in the webpage, I would apply implicit or explicit wait in the script to allow Selenium to wait for the element to load and then read it.

You can find the tutorials for implicit and explicit waits on the official Selenium website.

@Jagannatha, see if this works for you

0

I am not sure if this question is resolved, but I would like to share my thoughts. In TestNG, a method under @Test execute arbitrary or alphabetically. So, I believe the second method would execute first and hence dont find the element. You could try using "priority" attribute in @Test. I hope this helps. Thank you.

-1

I wonder how you even able to open the application?

Because you have an empty space in side string many places.

Problem #1

Change from : driver.get(" http://192.168.1.10:8080/sptest/home ");

to : driver.get("http://192.168.1.10:8080/sptest/home");

Problem #2

Change from:

driver.findElement(By.xpath(" .//*[@id='homeMainContainer']/div[1]/div/div/div[1]/div/ul/li[4]/a")).click();

to :

driver.findElement(By.xpath(".//*[@id='homeMainContainer']/div[1]/div/div/div[1]/div/ul/li[4]/a")).click();

Problem #3

Change from :

driver.findElement(By.xpath("   .//*[@id='campnEndInput']")).click();

to :

driver.findElement(By.xpath(".//*[@id='campnEndInput']")).click();

Hope this helps. If you still have issues, then update the question with your html code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.