1

I'm new to Selenium Webdriver using Python. I want to select the tag which contains 'Stats'. Tried multiple ways but failed as both the tags don't have any Id and have same class names as well. Please help me with the codes to select the tag which contains 'Stats' using Selenium webdriver with python.

List of few Trials, Even if the result is found. I'm unable to click it and the error message is Lists cannot be clicked.

driver.find_element_by_class_name("css173kae7").click()
driver.find_elements_by_link_text("stats/dashboard").click()
driver.find_elements_by_xpath("//*[contains(text(), 'Stats')]")

I've attached the image of the Inspect element of the code, please have a look at it.

(Updated) Image

Inspect element of the "Anchor Tags" from which one needs to be selected

wpercy
  • 9,636
  • 4
  • 33
  • 45
Sourav
  • 91
  • 1
  • 2
  • 8

4 Answers4

0

Based on the image of the HTML you shared, I would try this XPATH locator to select that element:

driver.find_element_by_xpath(".//a[@class='css-1qdedno' and @href='/stats/dashboard']")
PixelEinstein
  • 1,713
  • 1
  • 8
  • 17
  • It didn't work, I got an error. selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .//a[@class='css-1qdedno' and href='/stats/dashbord'] – Sourav Jul 26 '18 at 23:37
  • and I also updated `dashboard`, forgot the `a` in dashboard. I guess my spelling is off here at the end of my day. – PixelEinstein Jul 26 '18 at 23:40
  • Still the same, issue. Unable to locate the element. It's troubling me from past 2-3 hours. Thanks for your help though, PixelEinstein. – Sourav Jul 27 '18 at 00:21
  • If you could update your question with more of the ***HTML***, and not as a picture, and also the `code trials`, it would be much easier to debug this for you. Is there a way you could share the URL as well? – PixelEinstein Jul 27 '18 at 00:22
  • I'm really sorry, I cannot share the link as it belongs to my company and you would require my Id and Password to get into this page. However, I will upload a new image with all the required codes. Please, do take a look. Just to let you know, this is a JavaScript powered page. – Sourav Jul 27 '18 at 00:54
0

try this xpath

  //nav//a[contains(@href,'/stats/dashboard')]
murali selenium
  • 3,847
  • 2
  • 11
  • 20
0

Use this code:

driver.find_elements_by_xpath("//*[contains(text(), 'stats')]")

if you are looking for "stats" in href attribute use below code:

driver.find_elements_by_xpath("//*[contains(@href, 'stats')]")

Keep in mind, contains is case-sensitive.

If you want to do case-insensitive search, See here.

Raj
  • 664
  • 7
  • 23
  • I am able to find the element using driver.find_elements_by_xpath("//*[contains(text(), 'stats')]"). However, it has 2 elements and I'm unable to click() on the desired one. It gives me an error that list cannot be clicked. What would be the fix for this? – Sourav Jul 27 '18 at 10:30
  • AttributeError: 'list' object has no attribute 'click' – Sourav Jul 27 '18 at 10:38
  • Can you share the code, how are you trying to click? – Raj Jul 27 '18 at 13:15
  • driver.find_elements_by_xpath("//*[contains(text(), 'stats')]").click() – Sourav Jul 27 '18 at 19:30
  • you cannot click on a collection of elements. this code will return collection of elements `driver.find_elements_by_xpath("//*[contains(text(), 'stats')]")`. Iterate through collection to click on desired element. – Raj Jul 28 '18 at 08:17
0

You need to select the desired element by its index value after you get all the elements in a list:

elements= driver.find_elements_by_xpath("//*[contains(text(), 'stats')]")

Then identify and access the element by its index and call the click() function:

For example if the element is at elements[0], then

elements[0].click()