Reputation: 79
I am using selenium-java-2.53.jar to hit a website and perform a search operation. After the serach results come, I need to read the data. The website url is https://340bopais.hrsa.gov/coveredentitysearch . I put a value in the textbox for 340B ID as CH02123B and click 'Search'. After this the results are shown in a table. Till here, it works fine with selenium. But after this, when I try to read the data by using driver.findElement and By.xpath , I get an exception as org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element.
Below is the part of the HTML source from the search page which comes after I hit Search:
<tr class="rgRow" valign="top" id="ctl00_ContentPlaceHolder1_CoveredEntitySearchGrid_ctl00__0" role="row">
<td role="gridcell"><input id="ctl00_ContentPlaceHolder1_CoveredEntitySearchGrid_ctl00_ctl04_ClientSelectColumnSelectCheckBox" type="checkbox" name="ctl00$ContentPlaceHolder1$CoveredEntitySearchGrid$ctl00$ctl04$ClientSelectColumnSelectCheckBox" title="Select/Deselect Row" role="checkbox" aria-label="ClientSelectColumn checkbox" aria-checked="false"></td><td role="gridcell"><a title="CH02123B" href="cedetails/12136">CH02123B</a></td>
<td role="gridcell">CH</td>
<td role="gridcell">NEIGHBORHOOD HEALTH SERVICES CORPORATION</td>
<td role="gridcell">Neighborhood Health Center The Healthy Place</td>
<td role="gridcell">427 Darrow Ave</td>
<td role="gridcell">Plainfield</td>
<td role="gridcell">NJ</td>
<td role="gridcell">10/01/2004</td>
<td role="gridcell"> </td>
<td role="gridcell">02/13/2017</td>
</tr>
I want to read the TD which has content as 'NEIGHBORHOOD HEALTH SERVICES CORPORATION'. I got the Xpath from Chrome and used the same in my code. Still I get the exception. I have tried many combinations , but all in vain.
Below is my code:
StackApplication.class.getClassLoader().getResource("driver/chromedriver.exe").getFile();
WebDriver driverHL = new ChromeDriver();
driverHL.navigate().to("https://340bopais.hrsa.gov/coveredentitysearch");
driverHL.findElement(By.xpath("//*[@id=\"ContentPlaceHolder1_ceSearchCtrl_txt340BID\"]")).sendKeys("CH02123B");
driverHL.findElement(By.xpath("//*[@id=\"btnSearch\"]")).click() ;
WebDriverWait myDynamicElement = new WebDriverWait(driver, 30);
myDynamicElement.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//tr[@id=\"ctl00_ContentPlaceHolder1_CoveredEntitySearchGrid_ctl00__0\"]/td[4]")));
driverHL.findElement(By.xpath("//tr[@id=\"ctl00_ContentPlaceHolder1_CoveredEntitySearchGrid_ctl00__0\"]/td[4]"));
Complete stack trace of the exception (with some specific details overriden):
Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 30 seconds waiting for presence of element located by: By.xpath: //tr[@id="ctl00_ContentPlaceHolder1_CoveredEntitySearchGrid_ctl00__0"]/td[4]
Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09'
System info: host: 'hostName', ip: '<ip_address>', os.name: '<OS_Name>', os.arch: '<arch>', os.version: '<x.x>', java.version: '1.7.0_13'
Session ID: 7636dc2ad6277bd827467c2f40c3f322
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [<SomeMachineSpecificData>]
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:261)
at comm.myProject.MyDownloader.MyApplication.main(StackApplication.java:67)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//tr[@id="ctl00_ContentPlaceHolder1_CoveredEntitySearchGrid_ctl00__0"]/td[4]"}
(Session info: chrome=63.0.3239.108)
(Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09'
System info: host: 'hostName', ip: '<ip_address>', os.name: '<OS_Name>', os.arch: '<arch>', os.version: '<x.x>', java.version: '1.7.0_13'
*** Element info: {Using=xpath, value=//tr[@id="ctl00_ContentPlaceHolder1_CoveredEntitySearchGrid_ctl00__0"]/td[4]}
Session ID: 7636dc2ad6277bd827467c2f40c3f322
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [<specific info>]
at sun.reflect.GeneratedConstructorAccessor10.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:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:500)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:899)
at org.openqa.selenium.support.ui.ExpectedConditions.access$000(ExpectedConditions.java:41)
at org.openqa.selenium.support.ui.ExpectedConditions$6.apply(ExpectedConditions.java:181)
at org.openqa.selenium.support.ui.ExpectedConditions$6.apply(ExpectedConditions.java:178)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:238)
... 1 more
Upvotes: 0
Views: 1418
Reputation: 1165
More elaborative and easy to manage in future the reference code to get the desired texts td is below :
WebElement allItems = driver.findElement(By.xpath("//tr[@class='rgRow' and contains(@id,'CoveredEntitySearchGrid')]"));
List<WebElement> allTd = allItems.findElements(By.tagName("td"));
String stringToGet = "NEIGHBORHOOD HEALTH SERVICES CORPORATION";
for(WebElement elm : allTd){
if(elm.getText().equalsIgnoreCase(stringToGet)){
System.out.println(elm.getText());
break;
}
}
or use the below instead of for loop.
System.out.println(allTd.get(3).getText());
Upvotes: 0
Reputation: 466
Your code works fine for me, although I had to change this line...
WebDriverWait myDynamicElement = new WebDriverWait(driver, 30);
to
WebDriverWait myDynamicElement = new WebDriverWait(driverHL, 30);
Upvotes: 2
Reputation: 193108
As per the HTML
you have provided to read the TD which has content as NEIGHBORHOOD HEALTH SERVICES CORPORATION
you can try the following line of code :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//tr[@class='rgRow' and contains(@id,'CoveredEntitySearchGrid')]//following::td[4]")));
Upvotes: 0