Reputation: 3
I created a selenium code that automatized tests in my website.
In one screen I run a query, and the system shows in my IE screen the result of this query, as a table.
And then I need to select and click on a line of this table.
I try do this using the code below, but the result in Eclipse is that it cannot find the element 0531025836 even though it's present in the table results on my screen.
driver.findElement(By.xpath("//td[contains(.,'0531025836')]")).click();
Upvotes: 0
Views: 3161
Reputation: 10079
As per my understanding you are dealing with the webTables. If I am correct then there are multiple examples you can find on google for that like the one given below.
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://money.rediff.com/");
WebElement element=driver.findElement(By.id("allpage_links"));
List<WebElement> rowCollection=element.findElements(By.xpath("//*[@id='allpage_links']/tbody/tr"));
System.out.println("Number of rows in this table: "+rowCollection.size());
//Here i_RowNum and i_ColNum, i am using to indicate Row and Column numbers. It may or may not be required in real-time Test Cases.
int i_RowNum=1;
for(WebElement rowElement:rowCollection)
{
List<WebElement> colCollection=rowElement.findElements(By.xpath("td"));
//now here you have list of webelements of the table, you can perform
//any event on that as per your reqirement
int i_ColNum=1;
for(WebElement colElement:colCollection)
{
System.out.println("Row "+i_RowNum+" Column "+i_ColNum+" Data "+colElement.getText());
i_ColNum=i_ColNum+1;
}
i_RowNum=i_RowNum+1;
}
driver.close();
Upvotes: 0
Reputation: 8386
I've never seen that XPath before (but I'm not very proficient with XPath compared to some here).
If you are looking for a statement to select an element with exactly that text, try //td[.='0531025836']
.
If you are looking for a statement to select an element containing that text, try //td[contains(text(),'0531025836')]
Upvotes: 1