我正在尝试使用Selenium WebDriver
与Java
我想点击"Create Tournaments"
链接。我使用xpath
来识别该元素。但是我收到了错误
org.openqa.selenium.NoSuchElementException: no such element:
Unable to locate element: {"method":"xpath","selector":"//*[@id='createtournaments_li']/a"}
有人可以帮我吗??
代码:
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");}}
属性文件:
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
答案 0 :(得分:0)
这应该有效:
driver.findElement(By.xpath(".//*[@id='createtournaments_li']/a"));
确保代码不是<iframe>
的一部分,并且在执行之前已加载页面。
确保您在正确的页面上。在搜索元素之前,您的测试似乎没有登录。登录测试是一个单独的测试。创建锦标赛测试将从@BeforeTest
中的url状态开始答案 1 :(得分:0)
使用隐式等待可能会帮助你,我有同样的问题和隐含的等待工作就像一个魅力。 http://www.software-testing-tutorials-automation.com/2014/01/how-to-use-implicit-wait-in-selenium.html