-1

I am trying to automate a web application using Selenium WebDriver with Java

enter image description here

I want to click on "Create Tournaments"link. I used xpath for identifying that element. But I'm getting the error

org.openqa.selenium.NoSuchElementException: no such element: 
Unable to locate element: {"method":"xpath","selector":"//*[@id='createtournaments_li']/a"}

Can anybody help me in this..?

Code:

package User;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import junit.framework.Assert;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class QZO_Create_Tournament {

        WebDriver driver;
        private Properties properties;
        private final String PROPERTY_FILE_NAME = "constant.properties";

        public QZO_Create_Tournament() {
            properties = new Properties();
            try {
                InputStream inputStream = getClass().getClassLoader().getResourceAsStream(PROPERTY_FILE_NAME);
                if(null != inputStream)
                {
                    properties.load(inputStream);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @BeforeTest

        public void openBrowser()
        {
            System.setProperty("webdriver.chrome.driver", "D:\\MyProjects\\SeleniumTrials\\chromedriver_win32\\chromedriver.exe");
            driver= new ChromeDriver();
            driver.get(properties.getProperty("VAR_ADMIN_URL"));
            driver.manage().window().maximize();
        }

        @Test

        public void login() throws BiffException, IOException, InterruptedException{

            WebElement username=driver.findElement(By.xpath(properties.getProperty("VAR_ADMIN_USERNAME")));
            username.clear();
            username.sendKeys("admin");
            Thread.sleep(1000);
            WebElement password=driver.findElement(By.xpath(properties.getProperty("VAR_ADMIN_PASSWORD")));
            password.sendKeys("123456");
            Thread.sleep(1000);
            WebElement continuebutton=driver.findElement(By.xpath(properties.getProperty("VAR_ADMIN_CONTINUE")));
            continuebutton.click();
             Thread.sleep(20000);
        }


@Test

        public void createtournament(){

         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
         WebElement createtournament=driver.findElement(By.xpath(properties.getProperty("VAR_CREATETOURNAMENT")));
         // WebElement createtournament = driver.findElement(By.xpath("//a[@href='/Admin/CreateEditTournaments']"));
         createtournament.click();
         Select tournamenttype = new Select(driver.findElement(By.xpath(properties.getProperty("VAR_TOURNAMENTTYPE")))); 
         tournamenttype.selectByVisibleText("Planned Tournament");}}

Properties file :

VAR_ADMIN_USERNAME = //*[@id='username_txt']
VAR_ADMIN_PASSWORD = //*[@id='password_txt']
VAR_ADMIN_CONTINUE = //*[@id='main-section']/div/div/section[2]/div[2]/a/span
VAR_CREATETOURNAMENT = //*[@id='createtournaments_li']/a
VAR_TOURNAMENTTYPE = //*[@id='main-section']/div/div/div[2]/div[2]/ul/li/div/div/select
Amrutha
  • 301
  • 1
  • 7
  • 20
  • Try having a look at this http://stackoverflow.com/questions/12495723/using-xpath-wildcards-in-attributes-in-selenium-webdriver – Jsmith2800 Nov 17 '16 at 13:05
  • How many nodes does that xpath return? – Anton Nov 17 '16 at 13:52
  • @Jsmith2800 : I didn't get that – Amrutha Nov 18 '16 at 03:43
  • @Will: how can i count the number of nodes..? Dont know that. i'm new to selenium – Amrutha Nov 18 '16 at 03:51
  • @Amrutha in the lower left corner of the firepath window there will be something that says, "X matching node(s)." That's how you can be sure your xpath is finding one node only - the one you care about. If it's more than one then your xpath need some refinement. – Anton Nov 18 '16 at 13:54

2 Answers2

0

This should work:

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

Make sure the code is not part of an <iframe> and that the page is loaded before executing it.

Make sure you are on the right page. It looks like your test is not logging in before you search for the element. The login test is a separate test. The create tournament test will start from the url state in the @BeforeTest

Nick Vanderhoven
  • 3,018
  • 18
  • 27
0

use implicit wait may be it will help you out, i had a same problem and implicit wait work like a charm. http://www.software-testing-tutorials-automation.com/2014/01/how-to-use-implicit-wait-in-selenium.html