Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...