Home > Back-end >  Find correct xpath with selenium Python
Find correct xpath with selenium Python

Time:03-10

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="" ><i _ngcontent-oyt-c155="" ></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 ?

CodePudding user response:

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

'//a[@]//i[@]'

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.

CodePudding user response:

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
    
  • Related