Selenium Webdriver |无法找到'href'元素

时间:2015-02-27 10:11:06

标签: selenium xpath

我正在尝试使用Xpath和CSS在Selenium Webdriver中找到以下链接。

<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<tr>
<td class="cspbItmA">
<a class="cspbItm" target="_parent" href="../isp_track.asp?PARENT_MENU=Customer Order">Find Order</a>
</td>
</tr>

但是这些选项都没有,我无法运行脚本并被困在以下异常中:

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

driver.findElement(By.xpath(".//*[@id='cspbGrp0']/table/tbody/tr[2]/td/a")) .click();

 Starting ChromeDriver (v2.9.248315) on port 8139 Exception in thread
     "main" org.openqa.selenium.NoSuchElementException: no such element  
     (Session info: chrome=40.0.2214.115)   (Driver info:
     chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86) (WARNING: The
     server did not provide any stacktrace information) Command duration or
     timeout: 5.06 seconds For documentation on this error, please visit:
     http://seleniumhq.org/exceptions/no_such_element.html Build info:
     version: '2.42.0', revision:
     '5e824302019c86eae9c8c3ca9155e7307b410cf8', time: '2014-05-24
     09:48:41' System info: host: 'inl-279930-1', ip: '10.13.174.254',
     os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version:
     '1.7.0_55' Session ID: 04489825ed0cfc399afa1ffccb838870 Driver info:
     org.openqa.selenium.chrome.ChromeDriver Capabilities [{platform=XP,
     acceptSslCerts=true, javascriptEnabled=true, browserName=chrome,
     chrome={userDataDir=C:\Users\SUBRAM~1\AppData\Local\Temp\scoped_dir11808_15574},
     rotatable=false, locationContextEnabled=true, version=40.0.2214.115,
     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(NativeConstructorAccessorImpl.java:57)
        at
     sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
        at
     org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
        at
     org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
        at
     org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:596)
        at
     org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:349)
        at
     org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:446)
        at org.openqa.selenium.By$ByXPath.findElement(By.java:357)  at
     org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:341)
        at roll_forward_test.cpe_progression.main(cpe_progression.java:147)

请你帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

我的偏好是使用css而不是xpath,我通常会编写自己的选择器。因为它在表格中可能有点棘手(意味着您可能需要进一步遍历)但是从屏幕上的信息中我可以选择以下内容:

//by href
driver.findElement(By.cssSelector("a[href='../isp_track.asp?PARENT_MENU=Customer Order']"))

OR

//by class
driver.findElement(By.cssSelector("a.cspbItm"))

答案 1 :(得分:0)

发现问题,这是由于iFrame中存在元素造成的。

首先切换到iFrame时,现在能够找到元素。

谢谢你, 普利文