使用Seleniium的Python:无法找到要输入的元素

时间:2018-05-31 07:34:29

标签: python google-chrome selenium selenium-webdriver webdriver

我正在尝试使用Selenium在网站上找到一个元素,但我很难过:

我正在查看的页面是:https://corporate.axisbank.co.in/

所选元素的HTML如下所示:

<input name="FORM_CORPORATEID" type="text" onpaste="return false" ondrop="return false" ondrag="return false" oncopy="return false" oncut="return false" autocomplete="off" id="corporateId" class="cm-input-box has-gradient-border">

我尝试过使用Id,Xpath等但仍然遇到错误:

from selenium import webdriver

driver = webdriver.Chrome()
bankurl= "https://corporate.axisbank.co.in/"
driver.get(bankurl)
driver.find_element_by_name("FORM_CORPORATEID").send_keys("USERNAME")

我最终收到以下错误:

Traceback (most recent call last):
  File "C:/Users/alwyn.kotze/PycharmProjects/AlwynAuto1/Automations/Axis1.py", line 8, in <module>
    driver.find_element_by_name("FORM_CORPORATEID").send_keys("USERNAME")
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 489, in find_element_by_name
    return self.find_element(by=By.NAME, value=name)
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 957, in find_element
    'value': value})['value']
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 314, in execute
    self.error_handler.check_response(response)
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"FORM_CORPORATEID"}
  (Session info: chrome=66.0.3359.181)
  (Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.16299 x86_64)

可能导致此错误的原因是什么?

2 个答案:

答案 0 :(得分:0)

from selenium import webdriver
import time

driver = webdriver.Chrome()
bankurl= "https://corporate.axisbank.co.in/"

driver.get(bankurl)
time.sleep(5)

driver.find_element_by_id('corporateId').send_keys("USERNAME")

答案 1 :(得分:0)

根据您提供的用于查找元素并发送文本 USERNAME 的HTML,您需要引导 WebDriverWait 以使元素可点击如下:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Chrome()
    bankurl= "https://corporate.axisbank.co.in/"
    driver.get(bankurl)
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#corporateId"))).send_keys("USERNAME")
    
  • 浏览器快照:

axis_bank