如何测试href值是否为selenium的相对值

时间:2021-02-23 19:31:04

I want to test if the value in my href attributes in my a tags are relative paths using Selenium.

我想测试我的标签中的href属性中的值是否是使用Selenium的相对路径。

This is my HTML:

这是我的HTML:

<a href="/about/somepage/">my page</a>

I use selenium to select the a tag:

我用selenium来选择一个标签:

string hrefValue = _chromeDriver.FindElement(By.TagName("a")).GetAttribute("href");

This will return "http://mydomain/about/somepage/" even if the part "http://mydomain" is not in the HTML.

这将返回“http:// mydomain / about / somepage /”,即使“http:// mydomain”部分不在HTML中。

If I use GetAttribute("pathname") it will give what I want, however the GetAttribute("pathname") will only give the value "/about/somepage/" EVEN if the HTML looks like this:

如果我使用GetAttribute(“pathname”)它会给出我想要的东西,但GetAttribute(“pathname”)只会给出值“/ about / somepage /”,如果HTML看起来像这样:

<a href="http://myDomain/about/somepage/">my page</a>

So, how can I test if the actual value of the href attribute is relative or not to my website?

那么,我如何测试href属性的实际值是否与我的网站相关?

4 个解决方案

#1


5  

The easiest way to do this is to look for the A tag but also examine its href attribute and see if it starts with "http".

最简单的方法是查找A标记,同时检查其href属性,看它是否以“http”开头。

Console.WriteLine(driver.FindElements(By.CssSelector(a[href^='http'])).Any());

If this prints False then it's relative.

如果这打印False那么它是相对的。

#2


2  

Java version:

Java版本:

retrieve outerHTML of the element (link) and check whether it contains http:// in it.

检索元素(链接)的outerHTML并检查它是否包含http://。

    WebElement link = driver.findElement(By.linkText("my page"));

    String href = link.getAttribute("href");
    String outerHtml = link.getAttribute("outerHTML");

    System.out.println("href " + href);
    System.out.println("outerHtml " + outerHtml);

    if(outerHtml.contains("href=\"http://")){
        System.out.println(href + " is absolute path");
    }
    else{
        System.out.println(href + " is relative path");
    }

#3


2  

First, get all the links whose href start with http. Since, links may be both absolute or relative and you don't whether your desired one is starting with http so, an extra checking is necessary if list is greater than 1. Here is the code snippet-

首先,获取其href以http开头的所有链接。因为,链接既可以是绝对链接也可以是相对链接,而不是你想要的链接是否以http开头,如果列表大于1则需要额外的检查。这是代码片段 -

       List<WebElement> links = driver.findElements(By.cssSelector("a[href^='http']"));

        if (links.size() == 0) {
            System.out.println("relative");
        }else {
            boolean isAbsolute = false;
            for (WebElement link: links ) {
                if(link.getText().equals("Visit 2.com!")){
                    isAbsolute = true;
                    break;
                }

            }

            if (isAbsolute) {
                System.out.println("absolute");
            }else {
                System.out.println("relative");
            }
        }

#4


1  

Try this it should give you everything inside <a> and then you can check it :

尝试这个它应该给你在内的一切,然后你可以检查它:

string hrefValue = _chromeDriver.FindElement(By.TagName("a")).getAttribute("innerHTML")

string hrefValue = _chromeDriver.FindElement(By.TagName(“a”))。getAttribute(“innerHTML”)

Edit:

编辑:

try with outerHTML instead. It should give the whole thing

尝试使用outerHTML。它应该给整个事情

#1


5  

The easiest way to do this is to look for the A tag but also examine its href attribute and see if it starts with "http".

最简单的方法是查找A标记,同时检查其href属性,看它是否以“http”开头。

Console.WriteLine(driver.FindElements(By.CssSelector(a[href^='http'])).Any());

If this prints False then it's relative.

如果这打印False那么它是相对的。

#2


2  

Java version:

Java版本:

retrieve outerHTML of the element (link) and check whether it contains http:// in it.

检索元素(链接)的outerHTML并检查它是否包含http://。

    WebElement link = driver.findElement(By.linkText("my page"));

    String href = link.getAttribute("href");
    String outerHtml = link.getAttribute("outerHTML");

    System.out.println("href " + href);
    System.out.println("outerHtml " + outerHtml);

    if(outerHtml.contains("href=\"http://")){
        System.out.println(href + " is absolute path");
    }
    else{
        System.out.println(href + " is relative path");
    }

#3


2  

First, get all the links whose href start with http. Since, links may be both absolute or relative and you don't whether your desired one is starting with http so, an extra checking is necessary if list is greater than 1. Here is the code snippet-

首先,获取其href以http开头的所有链接。因为,链接既可以是绝对链接也可以是相对链接,而不是你想要的链接是否以http开头,如果列表大于1则需要额外的检查。这是代码片段 -

       List<WebElement> links = driver.findElements(By.cssSelector("a[href^='http']"));

        if (links.size() == 0) {
            System.out.println("relative");
        }else {
            boolean isAbsolute = false;
            for (WebElement link: links ) {
                if(link.getText().equals("Visit 2.com!")){
                    isAbsolute = true;
                    break;
                }

            }

            if (isAbsolute) {
                System.out.println("absolute");
            }else {
                System.out.println("relative");
            }
        }

#4


1  

Try this it should give you everything inside <a> and then you can check it :

尝试这个它应该给你在内的一切,然后你可以检查它:

string hrefValue = _chromeDriver.FindElement(By.TagName("a")).getAttribute("innerHTML")

string hrefValue = _chromeDriver.FindElement(By.TagName(“a”))。getAttribute(“innerHTML”)

Edit:

编辑:

try with outerHTML instead. It should give the whole thing

尝试使用outerHTML。它应该给整个事情