NoSuchElementException:消息:尝试通过 Selenium 和 Python 单击 VISA 按钮时无法定位元素 [英] NoSuchElementException: Message: Unable to locate element while trying to click on the button VISA through Selenium and Python

查看:22
本文介绍了NoSuchElementException:消息:尝试通过 Selenium 和 Python 单击 VISA 按钮时无法定位元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

这是我的代码:

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:UsersxAppDataLocalProgramsPythonPython37lib	hreading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:UsersxAppDataLocalProgramsPythonPython37lib	hreading.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-packagesseleniumwebdriver
emotewebdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:UsersxAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriver
emotewebdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:UsersxAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriver
emotewebdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:UsersxAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriver
emoteerrorhandler.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]

推荐答案

在我的机器人上创建结帐的按钮似乎是一个信用卡相关的领域,而且在历史上Credit Card 相关字段位于