2

I'm currently working on a project with Selenium and webdriver. So far so good I can navigate through the website using selenium without any problem however, I would need to click on a specific icon to slides through to a date and time table. Issue is when looking at the HTML I have no idea what to locate with Selenium in order to click on that icon.

HTML sample:

<a _ngcontent-oyt-c155="" class="btn btn-soft-primary day-selector-navbutton"><i _ngcontent-oyt-c155="" class="fa fa-chevron-right"></i></a>

Usually I'm locating items by name, ID or Class but in that case I don't know where to got with.

Should I look for the xpath instead ?

tboekhorst
  • 55
  • 1
  • 8
  • In my experience css selectors are the best way to get desired element/elements. You can find them bu right clicking on the element in the inspection tool and selecting copy css selector – Charalamm Mar 10 '22 at 07:42
  • @Charalamm XPath and css selectors having almost the same capabilities to locate elements on the DOM while XPath do has some advantages like locating parent node based on the child node and locating element based on it text value. – Prophet Mar 10 '22 at 07:50

2 Answers2

1

Accordingly to what you have shared here this XPath should work:

'//a[@class="btn btn-soft-primary day-selector-navbutton"]//i[@class="fa fa-chevron-right"]'

I can't be completely sure if this locator is unique or if there is no iframe there etc.
You will possibly need to add some kind of wait here too. If so you should preferably use Expected Conditions explicit waits.

Prophet
  • 32,350
  • 22
  • 54
  • 79
1

The desired element is a Angular element, so to click() on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-soft-primary.day-selector-navbutton > i.fa.fa-chevron-right"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn btn-soft-primary day-selector-navbutton']/i[@class='fa fa-chevron-right']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352