Подтвердить что ты не робот

Получить значение href (WebDriver)

Как получить значение из href?

например:

<div id="cont"><div class="bclass1" id="idOne">Test</div>

    <div id="testId"><a href="**NEED THIS VALUE AS STRING**">
    <img src="img1.png" class="clasOne" />
    </a>

</div>
</div>
</div>

Мне нужно это значение как строка.

Я пробовал с этим:

String e = driverCE.findElement(By.xpath("//div[@id='testId']")).getAttribute("href");
            JOptionPane.showMessageDialog(null, e);

Но просто возвращает значение NULL...

4b9b3361

Ответ 1

Вы указали свой элемент на 'div' вместо 'a'

Попробуйте приведенный ниже код

driverCE.findElement(By.xpath("//div[@id='testId']/a")).getAttribute("href");

Ответ 2

Если у вас несколько тегов привязки, следующий фрагмент кода поможет найти все ссылки, на которые указывает href

//find all anchor tags in the page
List<WebElement> refList = driver.findElements(By.tagName("a"));

   //iterate over web elements and use the getAttribute method to 
   //find the hypertext reference value.

    for(WebElement we : refList) {
        System.out.println(we.getAttribute("href"));
    }