Shay12345678
Shay12345678

Reputation: 113

A way to locate an element using Selenium

I am trying to locate an href element for some time now without any success.

String EMAIL_NOTIFICATIONS_PAGE = "[ng-href=\"#/settings/notifications/48\"]";

The number keeps changing. Sometimes selenium is able to locate the element and press it but sometimes it's not. I noticed that the number keeps changing.

I attached 2 screenshots.

Any suggestion how to locate an element that its href changed on the run?

the desired element

the locator

Upvotes: 0

Views: 77

Answers (5)

iamsankalp89
iamsankalp89

Reputation: 4749

You can use any of these XPaths:

//a[contains(text(),'Email notification settings')]

or:

//a[contains(@href,'#settings/notifications/18')]

or:

//a[@href='#settings/notifications/18']

Upvotes: 0

acikojevic
acikojevic

Reputation: 945

If this is changed dynamically you could try something like:

driver.findElement(By.xpath(".//a[contains(text(), 'Email notification settings')]")); 

This depends on text and it's not best practice, but it probably should work.

Upvotes: 1

tomasz.myszka
tomasz.myszka

Reputation: 111

also you can try

driver.FindElement(By.LinkText("Email notification settings"));

Upvotes: 0

Ratmir Asanov
Ratmir Asanov

Reputation: 6459

  1. You can use CSS selector for identification all a tags: .t6.s10.clickable.no-underline.

  2. Then access to the needed element by using the index in the list (array).

Upvotes: 0

utzcoz
utzcoz

Reputation: 659

You can find all href elements, and check its text to specific content, your occasion is Email notification settings. I always use this method if I can not have a clean and simply method to locate an element.

Upvotes: 0

Related Questions