Leo
Leo

Reputation: 189

I need to iterate table using css selectors

enter image description herescreenshot of the page

I am trying to automate a page using selenium webdriver. The page contains a table which has the following xpath.

Find below a sample xpath validation which works fine. I am iterating it through div as rows are considered as div under which there will be a table which has all the rows mentioned.

table = common.getObjectByXpath("html/body/div[4]/div[4]/div[1]/div/div/div[3]/div/div[2]/div/div/div/div");
rows = table.findElements(By.tagName("div"));

    for(int i=1;i<=rows.size();i++){
        if(driver.findElement(By.xpath("html/body/div[4]/div[4]/div[1]/div/div/div[3]/div/div[2]/div/div/div/div/div["+i+"]/table/tbody/tr/td[5]/span").getText().equals("endnode 11.1"))){
            System.out.println(" print Something");
        }
    }

It works fine with xpath. But I want to do it with css selector. I am attaching the sample format of the table. In the below figure, each div is considered as a row and under which you will see a table which has entries for columns.

Upvotes: 1

Views: 1319

Answers (1)

alecxe
alecxe

Reputation: 473833

The xpath you are using is a very fragile one since it starts right from the html element and heavily depends on the HTML structure of the page. Needless to say - it is huge and is not quite readable and easily understandable.

Instead rely on the element attributes - classes or ids. For example:

div#dojox_grid__View_11 div.dojoxGridContent div.dojoxGridRow

Upvotes: 1

Related Questions