-1

I'm trying to click on the 'Make a payment' icon but I'm not able to click on it using XPath.

I used:

driver.find_element_by_xpath("//div[@class='a11y appmagic-button-busy']//button[@class='appmagic-button-container no-focus-outline' and @title='Click here to make a payment']").click()

Snapshot:

enter image description here

JeffC
  • 22,180
  • 5
  • 32
  • 55
Vidhya
  • 1
  • Screenshots of the UI are great, screenshots of code or HTML are not. Please read why [a screenshot of code/HTML is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Paste the code/HTML and properly format it instead. – JeffC Feb 26 '23 at 01:58

2 Answers2

1

The problem with the XPath you are using is that it's looking for a BUTTON that's a *descendant* of a DIV but that DIV is actually a sibling of the desired BUTTON.

Given the HTML you posted, if you just remove the DIV portion of the XPath, the rest should work.

driver.find_element(By.XPATH, "//button[@class='appmagic-button-container no-focus-outline' and @title='Click here to make a payment']").click()

Having said that, there's an issue with XPaths like this. Your @class has to be an exact string match so if a different class gets added to that element or the order of the classes is changed, the XPath will no longer find the element. You'd be better off in this case using a CSS selector like

driver.find_element(By.CSS_SELECTOR, "button.appmagic-button-container.no-focus-outline[title='Click here to make a payment']").click()

or you can likely simplify it even more to

driver.find_element(By.CSS_SELECTOR, "button[title='Click here to make a payment']").click()
JeffC
  • 22,180
  • 5
  • 32
  • 55
0

The desired element is a dynamic element, so to click on the Make a payment 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, "button.appmagic-button-container.no-focus-outline[title='Click here to make a payment']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='appmagic-button-container no-focus-outline' and @title='Click here to make a payment']"))).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