简体   繁体   中英

How to get links who follow a pattern within specific section using xpath?

Here is what I'm trying to do:

I have some links on a webpage with this pattern:

  • /html/body/div[4]/div/div/section/div[2]/div[3]/div/ div 1 /div 1 /div/div[2]/a

  • /html/body/div[4]/div/div/section/div[2]/div[3]/div/ div 1 /div[2] /div/div[2]/a

  • /html/body/div[4]/div/div/section/div[2]/div[3]/div/ div 1 /div[3] /div/div[2]/a

  • /html/body/div[4]/div/div/section/div[2]/div[3]/div/ div 1 /div[4] /div/div[2]/a

However, I also have other links within the same web page that have a similar path but not exactly following the same pattern:

/html/body/div[4]/div/div/section/div[2]/div[3] /div/div[3] /div/div 1 /div 1 /div/div[2]/a

How can I get just the links which follow the first pattern displayed and ignore the other ones?

Ps: I'm using Selenium Webdriver and Java and this is the update question with the html for the links

 <div class="col-sm-6 half-tile">
        <div class="outside-caro">
            <div class="grey-overlay">
                <div class="inside-caro" style="background-image:url(' https://resources/images/metabolism.jpg'")>
                </div>
            </div>
            <div class="tile-content">
                <h4 class="module title-long-card">Healthy Weight Loss</h4>
                <p class="module line-clamp">This online eLearning programme is designed to help you make smart decisions when it comes to dieting and to be aware of the pitfalls.</p>
                <a class="more-button" href="/application/res-courses/overview?id=23">Learn More<i style="font-size: 10px;padding-left: 5px; "class="fa fa-chevron-right" aria-hidden="true"></i></a>
            </div>
        </div>
    </div>

在此处输入图片说明

Thanks very much.

I'm not quite following what you're really hoping for from your description, but I can make some guesses.

The quick answer is, just always give the full path .

But there are ways to make things a little easier to code. There are a couple ways you can create a pointer on the page and to only look for things beyond that point. The most straightforward is using simple string concatenation:

String pointer = "/html/body/div[4]/div/div/section/div[2]/div[3]/div/div[1]";
WebElement tag1 = driver.findElement(By.xpath(pointer + "/div[1]/div/div[2]/a"));
WebElement tag2 = driver.findElement(By.xpath(pointer + "/div[2]/div/div[2]/a"));

The other is to declare that pointer as a WebElement, and then use it as a base for all future findElements:

WebElement pointer = driver.findElement(By.xpath("/html/body/div[4]/div/div/section/div[2]/div[3]/div/div[1]"));
WebElement tag1 = pointer.findElement(By.xpath("./div[1]/div/div[2]/a"));
WebElement tag2 = pointer.findElement(By.xpath("./div[2]/div/div[2]/a"));

Note the dot at the beginning of the xpath to say "Use this node as your starting point".

Now, what I think you're really trying to accomplish to to make a list of all the anchors, not just pick them one by one. As in "get all the link that match one pattern but not a different but similar pattern" . For that, you could just do a variation of either of the two above methods. For instance:

WebElement pointer = driver.findElement(By.xpath("/html/body/div[4]/div/div/section/div[2]/div[3]/div/div[1]"));
List<WebElement> tags = pointer.findElement(By.xpath("./div/div/div[2]/a"));

This will pull in all the links that match the pattern into a List. There are a couple things to take note:

  • The first element is just div , not div[1] and div[2] . since that seems to be the only thing changing in the pattern.
  • Most likely, the language you will use to script this is 0-indexed. So div[1] is tags.get(0) .

I have a solution to problem and i hope it will help you.

You just have to identify a single parent for all 4 links that you mentioned above. And i feel you can use this locator as parent node /html/body/div[4]/div/div/section/div[2]/div[3]/div/div[1]/div[1] .

Please find my code..

    System.setProperty("webdriver.chrome.driver","Drivers/chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.abodeqa.com/2015/08/26/finding-child-elements-in-webdriver-using-findelements/");
    Thread.sleep(3000);
    WebElement parent = driver.findElement(By.xpath("//section[@class='secondary clearfix']"));
    List<WebElement>childernNodes = parent.findElements(By.xpath("./aside//a"));
    System.out.println("Total: "+childernNodes.size());
    for(WebElement value: childernNodes){
       System.out.println(value.getAttribute("href")); 
    }
 WebElement pointer = driver.findElement(By.xpath("/html/body/div[4]/div/div/section/div[2]/div[3]/div/div[1]"));

// Generic path to simulate the change in the xpath for the elements of the following pattern:
// WebElement tag1 = pointer.findElement(By.xpath("./div[1]/div/div[2]/a"));
// WebElement tag2 = pointer.findElement(By.xpath("./div[2]/div/div[2]/a"));

List<WebElement> linksList = pointer.findElements(By.xpath("./div/div/div[2]/a")); 
    for (WebElement link : linksList) {
        System.out.println(link.getAttribute("href"));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM