javascriptpythonselenium-webdriverweb-scrapingshadow-root

How to click the button on Visa exchange rate calculator using Selenium?


The revised the code from Shawn's answer to the question below to fill in another hidden field, Bank Fee.

How to find and fill in the hidden currency input field on a Visa exchange rate calculator using Selenium?

The filed I want to fill in.

However, after I revised the figure and performed the click on Calculate Conversion button, I received a ElementNotInteractableException warning.

The warning message

My code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from time import sleep


driver = webdriver.Chrome()
driver.get('https://usa.visa.com/support/consumer/travel-support/exchange-rate-calculator.html')
wait = WebDriverWait(driver, 30)
accept_button_element = wait.until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Accept']")))
accept_button_element.click()

shadow_root = driver.find_element(By.XPATH, "//dm-calculator").shadow_root

enter_amount = shadow_root.find_element(By.ID, "input_amount_paid")
enter_amount.send_keys("1")

from_dropdown = shadow_root.find_element(By.ID, "autosuggestinput_from")
from_dropdown.click()
shadow_root.find_element(By.ID, "listbox-item-157").click()

to_dropdown = shadow_root.find_element(By.ID, "autosuggestinput_to")
to_dropdown.click()
shadow_root.find_element(By.ID, "listbox-item-0").click()

fee_edit = shadow_root.find_element(By.CLASS_NAME, 'vs-link-cta.vs-calculator-edit-link')
fee_edit.click()

bank_rate = to_dropdown = shadow_root.find_element(By.ID, "input_bank_rate")
bank_rate.send_keys(Keys.CONTROL, 'a')
sleep(2)
bank_rate.send_keys(Keys.BACKSPACE)
bank_rate.send_keys('0')

# clicks on Calculate Conversion button
shadow_root.find_element(By.CLASS_NAME, 'vs-btn.vs-btn-primary').click()

Is there any way to make it work?

I tried to use the mouse action but it didn't work.


Solution

  • The Error is generated from the last line in your code which is :

    # clicks on Calculate Conversion button
    shadow_root.find_element(By.CLASS_NAME, 'vs-btn.vs-btn-primary').click()
    

    Replace it with the below line to make it work:

    # clicks on Calculate Conversion button
    shadow_root.find_elements(By.CSS_SELECTOR, 'div.vs-container')[-1].find_elements(By.TAG_NAME, 'button')[0].click()
    sleep(5)
    

    First, we find the last element with the class vs-container inside the shadow_root because it contains the Calculate Conversion button and then inside it, there are two buttons Calculate Conversion and Reset. So we click on the first button.

    I hope this solves your problem!