Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用selenium根据页面的href属性选择页面中的链接?_Java_Eclipse_Selenium Webdriver - Fatal编程技术网

Java 如何使用selenium根据页面的href属性选择页面中的链接?

Java 如何使用selenium根据页面的href属性选择页面中的链接?,java,eclipse,selenium-webdriver,Java,Eclipse,Selenium Webdriver,我是硒的新手。 我正在尝试根据“href”属性选择链接。我不能在这里使用xpath,因为每次加载页面时链接都会在页面上移动。请帮帮我。 谢谢。您可以通过.linkText使用By.linkText或By.partialLinkText例如 WebElement cheese=driver.findElement(By.partialLinkText(“cheese”) 这个示例是从中提取的,您可以使用xpath构造方法或以下方法 public void clickLink(WebDriver

我是硒的新手。 我正在尝试根据“href”属性选择链接。我不能在这里使用xpath,因为每次加载页面时链接都会在页面上移动。请帮帮我。
谢谢。

您可以通过.linkText使用
By.linkText
By.partialLinkText
例如

WebElement cheese=driver.findElement(By.partialLinkText(“cheese”)

这个示例是从

中提取的,您可以使用xpath构造方法或以下方法

public void clickLink(WebDriver driver) {

    List<WebElement> aList = driver.findElements(By.tagName("a"));

    for (WebElement el : aList) {

        // if (el.getAttribute("href").contains("your href partial value")

        if (el.getAttribute("href")
                .equalsIgnoreCase("your full hreff text")) {
            el.click();
            break;
        }
    }
}
public void点击链接(WebDriver驱动程序){
列表列表=driver.findElements(按.tagName(“a”));
for(WebElement el:aList){
//如果(el.getAttribute(“href”)包含(“您的href部分值”)
if(el.getAttribute(“href”)
.equalsIgnoreCase(“您的完整hreff文本”)){
el.click();
打破
}
}
}

根据需要使用if条件。可以是部分比较,也可以是完全比较。

使用xpath//a[contains(@href,'yourref')]当您使用//时,它意味着一个相对的xpath,因此无论层次结构如何,如果您的href是唯一的,这都可以为您完成这项工作。

使用css选择器,例如-driver.findElement(by.cssSelector(“a[href=(您的href)]”

如果您使用的是webdriver,则可以使用
by.linkText()
并指定您的
锚定标签中的文本。我无法使用,锚定标签中的文本对于此处的所有链接都是相同的。:(Jaydev,请您解释一下,并给出一个示例。对于cheese>对于cheese>对于Jaydev:如果标签中有相同的文本,那么它们必须继续(参考)到同一个页面。而且如果每次标记在周围移动,那么单击哪个页面无关紧要。因此,您可以安全地使用
By.linkText()
方法。我不能使用该方法。在我的例子中,锚定标记中的文本对于所有链接都是相同的,唯一的属性是href,每个链接都不同。