我正在尝试使用Selenium WebDriver在网站上输入ID和密码,但我的代码不起作用。它似乎无法找到一个元素。
我检查了HTML代码并找到了值。
这是我的Python代码
from selenium import webdriver
driver=webdriver.Chrome('C:\Chrome_Driver\chromedriver.exe')
driver.get('http://hisnet.handong.edu/')
sleep(0.5)
driver.find_element_by_name('id').send_keys('ㅁ')
sleep(0.5)
driver.find_element_by_name('password').send_keys('ㅁ')
这是HTML代码
input type="text" style="color:#000000; height: 16px; width:138px;ime-mode:inactive" name="id" autocomplete="off" tabindex="1" placeholder="아이디를 입력하십시오." value=""
有嵌套的框架要协商
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
url = 'http://hisnet.handong.edu/'
driver = webdriver.Chrome()
driver.get(url)
driver.switch_to.frame(driver.find_element_by_css_selector("[name=MainFrame]"))
driver.switch_to.frame(driver.find_element_by_css_selector("[name=MainFrame]"))
driver.find_element_by_css_selector("[name=id").send_keys("banana")
driver.find_element_by_css_selector("[type=password]").send_keys("orange")
您可以使用框架的等待条件:
WebDriverWait(driver, 5).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"[type=password]")))