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

标签 python selenium xpath css-selectors webdriverwait

我无法点击此按钮在我的机器人上创建结账。 我想点击图片进入另一个页面。

<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>
这是我的代码:

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

我收到此错误:

 File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\threading.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:\Users\x\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.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]

最佳答案

在我的机器人上创建结帐的按钮似乎是与信用卡相关的字段,并且历史上与信用卡相关的字段位于 <iframe> 内.

您可以在以下位置找到一些相关讨论:

因此,如果所需的元素位于 <iframe> 内,那么您必须:

  • 引发WebDriverWait以使所需的框架可用并切换到它
  • 引发WebDriverWait以使所需的元素可点击
  • 您可以使用以下任一解决方案:

    • 使用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()
      
    • 使用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()
      
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

关于python - NoSuchElementException : Message: Unable to locate element while trying to click on the button VISA through Selenium and Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54970347/

相关文章:

selenium - TestNG Assert失败如何截图?

reactjs - 使用 Selenium 从输入组合框中选择一个值,使用 xpath 作为reachjs 控件

python - 如何在 Python 中查看和打印 PDF?

python - 使用 Python 处理来自 MySQL 的数据

python - 有关 Python 代码的帮助

python - 隐式方程和 Mayavi

python - 如何使用Python + Selenium从网站下载特定文件

python - 为什么 Selenium 的 wait.until_not(EC.invisibility_of_element_ located) 等待加载器消失的时间太长?

java - 为什么文本节点值之间没有空格?

javascript - Selenium 网络驱动程序 : wait until text is present on page