1

我的 Web 应用程序有一个收件箱,就像 gmail 收件箱一样,我的目标是单击一个邮件项目并验证其中的内容。

我的收件箱中邮件的外部 HTML 如下

<a href="read_body.php?mailbox=INBOX&amp;passed_id=2&amp;startMessage=1">Complete Registration Request- My WebSite</a>

我写了一个java代码

driver.findElement(By.partialLinkText("Complete Registration Request- My WebSite")).click();

(ps:我也尝试过使用linkText)。

另一种方法是

  @FindBy(how=How.LINK_TEXT,using="link=Complete Registration Request- My WebSite")
  @CacheLookup
  private WebElement regMailLink;

内部方法

 regMailLink.click();

在这两种情况下,输出如下。

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"partial link text","selector":"Complete Registration Request- My WebSite"}
Command duration or timeout: 31.44 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.28.0', revision: '18309', time: '2012-12-11 20:21:18'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_39'
Session ID: 2baf4e73-c070-4046-8723-b4ae430a827d
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox, rotatable=false, locationContextEnabled=true, version=16.0, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=true, 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:187)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:533)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:302)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByPartialLinkText(RemoteWebDriver.java:347)
    at org.openqa.selenium.By$ByPartialLinkText.findElement(By.java:265)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:294)
    at stabilix.org.hc.MailCode.testValidateEmail(MailCode.java:74)
    at stabilix.org.hc.createUserPageTest.NewUsrStage1(createUserPageTest.java:48)
    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:80)
    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:128)
    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:1203)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
    at org.testng.TestNG.run(TestNG.java:1036)
    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)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"partial link text","selector":"Complete Registration Request- My WebSite"}
Build info: version: '2.28.0', revision: '18309', time: '2012-12-11 20:21:18'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_39'
Driver info: driver.version: unknown
    at <anonymous class>.<anonymous method>(file:///C:/DOCUME~1/moshe/LOCALS~1/Temp/anonymous7889540798209195994webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:8251)
    at <anonymous class>.<anonymous method>(file:///C:/DOCUME~1/moshe/LOCALS~1/Temp/anonymous7889540798209195994webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:382)
---
---
4

2 回答 2

2

好吧,好吧。这仍然是比建议更多的想法:

根据您的评论,该应用程序具有框架。我认为这个链接将在框架内。这个框架应该有它的名字:

 <frame name="main"> //my guess
   //...
   <a href="read_body.php?mailbox=INBOX&amp;passed_id=2&amp;startMessage=1">Complete Registration Request- My WebSite</a>
   //...
 </frame>

所以,你需要做的第一件事就是将驱动程序切换到这个框架

driver.switchTo.frame("main");

然后你可以执行搜索

driver.findElement(By.partialLinkText("Complete Registration Request- My WebSite")).click();

注意框架名称是我自己的猜测,所以请不要复制粘贴我的代码。将其用作指南;)

于 2013-02-26T10:21:35.850 回答
0

这里实际上我们可以尝试使用下面给出的 CSS 选择器

css=a[href*='收件箱']

请让我知道这个 CSS 脚本是否正常工作。

于 2014-04-08T07:04:16.923 回答