简体   繁体   English

如何使用xpath获取遵循特定部分中的模式的链接?

[英]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 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 [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 [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 / 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 / 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 Ps:我正在使用Selenium Webdriver和Java,这是链接的html的更新问题

 <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,然后将其用作将来所有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". 请注意xpath开头的点说“使用此节点作为起点”。

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] . 第一个元素只是div ,而不是div[1]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. 最有可能的是,您用来编写脚本的语言是0索引的。 So div[1] is tags.get(0) . 因此div[1]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. 您只需为上面提到的所有4个链接标识一个单亲。 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] . 我觉得您可以将此定位器用作父节点/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"));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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