Find an element by text and get xpath - selenium webdriver junit

后端 未结 11 752
情深已故
情深已故 2020-12-03 04:17

I have a table with 9 rows and 6 columns in my webpage. I want to search for a text \"MakeGoodDisabled-Programwise_09_44_38_461(n)\" and get the xpath of the cell. I have us

11条回答
  •  心在旅途
    2020-12-03 04:33

    You can also use this to crawl up and generate the xpath:

    Call the method below with

    generateXPATH(element, "");
    

    The output will be something like:

    /html[1]/body[1]/div[5]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/form[1]/div[2]/div[1]/input[2]
    

    METHOD

    private String generateXPATH(WebElement childElement, String current) {
        String childTag = childElement.getTagName();
        if(childTag.equals("html")) {
            return "/html[1]"+current;
        }
        WebElement parentElement = childElement.findElement(By.xpath("..")); 
        List childrenElements = parentElement.findElements(By.xpath("*"));
        int count = 0;
        for(int i=0;i

提交回复
热议问题