NoSuchElementException: Message: Unable to locate element while trying to click on the button VISA through Selenium and Python

I can’t click on this button to create a checkout on my bot.
I want to click the image to get another page.

<label for="VISA" class="choiceLabel">
            
                <input type="radio" class="visuallyhidden" name="cardTypeRadio" id="VISA" value="VISA" title="VISA" onclick="validateAndSubmit('VISA');">
                    <span class="imgElt xh-highlight" onclick="validateAndSubmit('VISA');">
                <img src="/static/2.15.0.1/images/type-carte/visa.png" alt="VISA" title="Visa">
                </span>
            <span class="txtElt">Visa</span>
          </label>

this is my code:

try:
             check = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID,"VISA" )))
            
             print ("Page is ready!")
             visa = driver.find_elements_by_xpath("label[@class='choiceLabel'][4]")
             visa.click()


        except TimeoutException:
             print ("Loading took too much time!")
             return check

im getting this error:

 File "C:UsersxAppDataLocalProgramsPythonPython37libthreading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:UsersxAppDataLocalProgramsPythonPython37libthreading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "c:/Users/pietro/Documents/monitor/x/bot.py", line 48, in all
    visa = driver.find_element_by_xpath("label[@class='choiceLabel'][4]")
  File "C:UsersxAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriverremotewebdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:UsersxAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriverremotewebdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:UsersxAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriverremotewebdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:UsersxAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriverremoteerrorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: label[@class='choiceLabel'][4]

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The button to create a checkout on my bot seems to be a Credit Card related field and historically Credit Card related fields resides within <iframe>.

You can find a couple of relevant discussions in:

So if the the desired element is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use either of the following solutions:
    • Using CSS_SELECTOR:
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe_css_selector")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='VISA']"))).click()
    • Using XPATH:
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe_xpath")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='VISA']"))).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

Method 2

The xPath you are searching for might look more like this:

//input[@name='cardTypeRadio' and @value='VISA']

If this doesn’t give you the unique element we can refine it a bit.

Method 3

Probably your xpath is wrong.Try this below code.Hope this helps.

driver.find_element_by_xpath("//input[@name='cardTypeRadio']").click()

OR

driver.find_element_by_xpath("//input[@id='VISA']").click()

If the webdriver click do not work.Try JavaScript to click.

driver.execute_script("arguments[0].click();", driver.find_element_by_xpath("//input[@name='cardTypeRadio']"))

driver.execute_script("arguments[0].click();", driver.find_element_by_xpath("//input[@id='VISA']"))


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x