Nikita Kozlovsky
Nikita Kozlovsky

Reputation: 85

How should i check title using Selenium?

Hey I can't resolve little problem. I have to check title of element on webpage using cssSelector or xpath. But I can't understand what command I should use to read elements title. Here is my code:

assertEquals(driver.findElement(By.xpath("//*[@id=\"main-panel\"]/div[16]/a/dl/dd[1]")), "Создание, удаление и модификция пользователей, имеющих право доступа к Jenkins");
assertEquals(driver.findElement(By.cssSelector("a[title=\"Управление пользователями\"] > dl > dd")), "Управление пользователями");

Xpath and cssSelector are correct. Here is HTML code if you need it

<a href="securityRealm/" title="Управление пользователями"><img src="/static/4be99593/images/48x48/user.png" alt="manage-option" class="icon"><dl><dt>Управление пользователями</dt><dd>Создание, удаление и модификция пользователей, имеющих право доступа к Jenkins</dd><dd></dd></dl></a>

Upvotes: 1

Views: 455

Answers (1)

Andrei
Andrei

Reputation: 5647

This will prove if the title of element is right:

assertEquals(driver.findElement(By.cssSelector("a[title='Управление пользователями']")).getAttribute("title"), "Управление пользователями");

But I would say, if element with this cssSelector: a[title='Управление пользователями'] was found, it is already means that it has title Управление пользователями. There is no need to prove it one more time with assert

Or if you want to check the text value of element, you can use this xPath:

//a/dl[contains(., 'Управление пользователями') and contains(., 'Создание, удаление и модификция пользователей, имеющих право доступа к Jenkins')]

and you could prove if such element exists like this:

driver.findElement(By.xpath("//a/dl[contains(., 'Управление пользователями') and contains(., 'Создание, удаление и модификция пользователей, имеющих право доступа к Jenkins')]"));

if element will be not found, it will throw an exception.

Upvotes: 1

Related Questions