根据类名查找元素时 因为类名包含空格导致报错
# 导包 import time from selenium import webdriver from selenium.webdriver.common.by import By from time import sleep # 引用谷歌驱动 driver=webdriver.Chrome() driver.get(r"D:\python-pro\baseapitest\xpath\html.html") # 输入密码 driver.find_element(By.CLASS_NAME,"in fo").send_keys(12345) time.sleep(5) driver.quit()
因为类名包含空格导致报错
解决办法
第一种
去掉空格和之后的内容
# 导包 import time from selenium import webdriver from selenium.webdriver.common.by import By from time import sleep # 引用谷歌驱动 driver=webdriver.Chrome() driver.get(r"D:\python-pro\baseapitest\xpath\html.html") # 输入密码 driver.find_element(By.CLASS_NAME,"in").send_keys(12345) time.sleep(5) driver.quit()
第二种 更换方法 CSS_SELECTOR 并且前面加点,空格也替换为点
# 导包 import time from selenium import webdriver from selenium.webdriver.common.by import By from time import sleep # 引用谷歌驱动 driver=webdriver.Chrome() driver.get(r"D:\python-pro\baseapitest\xpath\html.html") # 输入密码 driver.find_element(By.CSS_SELECTOR,".in.fo").send_keys(12345) time.sleep(5) driver.quit()
# 导包 import time from selenium import webdriver from selenium.webdriver.common.by import By from time import sleep # 引用谷歌驱动 driver=webdriver.Chrome() driver.get(r"D:\python-pro\baseapitest\xpath\html.html") # 输入第一个密码 driver.find_element(By.CSS_SELECTOR,".in.fo1").clear() driver.find_element(By.CSS_SELECTOR,".in.fo1").send_keys(12345) # 输入第二个密码 driver.find_element(By.CSS_SELECTOR,"input.in.fo2").clear() driver.find_element(By.CSS_SELECTOR,"input.in.fo2").send_keys(12345) time.sleep(5) driver.quit()
第三种 xpath 完胜
# 导包 import time from selenium import webdriver from selenium.webdriver.common.by import By from time import sleep # 引用谷歌驱动 driver=webdriver.Chrome() driver.get(r"D:\python-pro\baseapitest\xpath\html.html") # 输入第一个密码 driver.find_element(By.XPATH,"//input[@class='in fo1']").clear() driver.find_element(By.XPATH,"//input[@class='in fo1']").send_keys(1) time.sleep(5) driver.quit()