1

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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

6 Comments

"dojox_grid__View_11" will keep on changing and is there a way we could have a pattern match for this string alone?
@Leo yup, for example a starts-with version: div[id^=dojox_grid__View_]
Thanks. It woks fine. But I am stuck with another issue. Looks like if the table grows beyond 75 rows, it creates a new div with role="presentation" is created. This gets created only when i scroll beyond 75 rows. Since a new one is created only when you scroll I am not sure how to handle it. Attached the screenshot for both. First screenshot is after scrolling and second screenshot is before
@Leo could you please elaborate the latest problem into a separate question describing your desired result? I think it makes sense since it looks like a separate new problem. Throw me a link here also. Thank you!
Sure. I will post it and send you the link
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.