Py学习  »  Python

无法单击按钮python selenium

Pietro Riccucci • 7 年前 • 2227 次点击  

我无法单击此按钮在我的机器人上创建签出。 我想点击图片来获得另一个页面。

<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]
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40350
文章 [ 3 ]  |  最新文章 7 年前
KunduK
Reply   •   1 楼
KunduK    7 年前

可能是你的 xpath 是错误的。请尝试下面的代码。希望有帮助。

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

或

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

如果 webdriver 单击“不工作”。尝试使用javascript单击。

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']"))
C. Peck
Reply   •   2 楼
C. Peck    7 年前

您正在搜索的xpath可能更像这样:

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

如果这没有给你唯一的元素,我们可以改进一点。

DebanjanB
Reply   •   3 楼
DebanjanB    7 年前

这个 按钮在我的机器人上创建签出 好像是 信用卡 相关领域和历史 信用卡 相关字段位于 <iframe> .

您可以在中找到一些相关的讨论:

所以如果所需的元素在 <iFrAME> 所以你必须:

  • 诱导 网络驱动器 为了想要的 框架可用并切换到它 .
  • 诱导 网络驱动器 为了想要的 可点击的元素 .
  • 您可以使用以下任一解决方案:

    • 使用 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