package com.OSCom.CommonMethods;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
public class CommonMethods {
private WebDriver driver;
public CommonMethods(WebDriver driver)
{
this.driver=driver;
}
public void clickElement(WebElement slocator)
{
try
{
slocator.click();
System.out.println(slocator+" clicked");
}
catch(Exception e)
{
System.out.println(slocator+ " NOT clicked");
}
}
}
所以我有这个简单的方法可以点击selenium中的链接。如果我这样做:
slocator.click();
然后一切都运行良好。但是,当我把它们放在try和catch块中时,它给了我这个错误:
org.openqa.selenium.NoSuchElementException: no such element
(Session info: chrome=37.0.2062.120)
(Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 30.12 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:03'
System info: host: 'Khoi-PC', ip: '192.168.1.133', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_67'
Session ID: 9b7e97ea09696dfc78ade8e6503a548a
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=C:\Users\Khoi\AppData\Local\Temp\scoped_dir6056_19855}, rotatable=false, locationContextEnabled=true, version=37.0.2062.120, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=false, takesScreenshot=true}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByLinkText(RemoteWebDriver.java:401)
at org.openqa.selenium.By$ByLinkText.findElement(By.java:242)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:59)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:34)
at com.sun.proxy.$Proxy10.toString(Unknown Source)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.OSCom.CommonMethods.CommonMethods.clickElement(CommonMethods.java:30)
at com.OSCom.page.HomePage.clickLogIn(HomePage.java:26)
at com.OSCom.tests.LogInTest.logInTest(LogInTest.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
抱歉,我将发布所有代码。我害怕引起混乱。这是将调用CommonMethods的类:
package com.OSCom.page;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import com.OSCom.CommonMethods.CommonMethods;
public class HomePage {
private WebDriver driver;
CommonMethods CM;
@FindBy(how=How.LINK_TEXT, using = "log yourself in")
private WebElement logIn;
public HomePage(WebDriver driver)
{
this.driver = driver;
}
public void clickLogIn() throws InterruptedException
{
CM = new CommonMethods(driver);
CM.clickElement(logIn);
}
}
这是我使用testNg运行的测试类
package com.OSCom.tests;
import java.io.IOException;
import jxl.read.biff.BiffException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.OSCom.CommonMethods.CommonMethods;
import com.OSCom.page.HomePage;
import com.OSCom.page.SignInPage;
public class LogInTest {
private WebDriver driver;
CommonMethods CM;
public String URLName = "link here";
private String dataFilePath = "S:\\Selenium\\Assignment\\TestData.xls";
private String[][] data;
private int dataRow = 2;
private int dataCol = 2;
@BeforeMethod(alwaysRun=true)
public void setUp()
{
CM = new CommonMethods(driver);
driver=CM.openBrower("Chrome");
CM.navigateURL(URLName);
}
@Test
public void logInTest() throws InterruptedException, BiffException, IOException
{
HomePage homepage = new HomePage(driver);
PageFactory.initElements(driver, homepage);
homepage.clickLogIn();
data = CM.readExcel(dataRow, dataCol, dataFilePath);
SignInPage signIn = PageFactory.initElements(driver, SignInPage.class);
signIn.enterInfo(data[1][0],data[1][1]);
}
@AfterMethod
public void done()
{
}
}