Hi,
the code below is getting only first row records from the web table. could you pls help me know the mistake I made.
public class WebTable {
static WebDriver driver;
public static void main(String[] args) {
System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY, "null");
System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY,"true");
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+ "/drivers/chromedriver");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://money.rediff.com/gainers");
int i= getRowNumWithCellData("Baba Arts");
System.out.println(i);
String price = driver.findElement(By.xpath("//table[@class='dataTable']/tbody/tr["+i+"]/td[4]")).getText();
System.out.println(price);
}
public static int getRowNumWithCellData(String data){
List<WebElement> rows = driver.findElements(By.xpath("//table[@class='dataTable']/tbody/tr"));
for (int i = 0; i < rows.size(); i++) {
WebElement row = rows.get(i);
List<WebElement> cell = row.findElements(By.tagName("td"));
for (int j = 0; j < cell.size();j++){
String cellValue = cell.get(j).getText();
if(cellValue.equals(data));
return ++i;
}
}
return -1;
}
Instructor
Ashish Thakur Replied on 11/09/2019
Did you try debugging the code?
Additionally, I can see that you are trying to lookup for Baba Arts only. This means that you are trying to look up information for this company only.
found the issue and resolved it.