旧版本模式:
# 以下inputTag任选其一,其他注释掉
inputTag = driver.find_element_by_id("value") # 利用ID查找inputTags = driver.find_element_by_class_name("value") # 利用类名查找inputTag = driver.find_element_by_name("value") # 利用name属性查找inputTag = driver.find_element_by_tag_name("value") # 利用标签名查找inputTag = driver.find_element_by_xpath("value") # 利用xpath查找inputTag = driver.find_element_by_css_selector("value") # 利用CSS选择器查找
新版本模式:首先引入包
from selenium import webdriver
from selenium.webdriver.common.by import By
然后方法更改为:
# inputTag = driver.find_element_by_id("value") # 利用ID查找
# 改为:
inputTag = driver.find_element(By.ID, "value")# inputTags = driver.find_element_by_class_name("value") # 利用类名查找
# 改为:
inputTag = driver.find_element(By.CLASS_NAME, "value")# inputTag = driver.find_element_by_name("value") # 利用name属性查找
# 改为:
inputTag = driver.find_element(By.NAME, "value")# inputTag = driver.find_element_by_tag_name("value") # 利用标签名查找
# 改为:
inputTag = driver.find_element(By.TAG_NAME, "value")# inputTag = driver.find_element_by_xpath("value") # 利用xpath查找
# 改为:
inputTag = driver.find_element(By.XPATH, "value")# inputTag = driver.find_element_by_css_selector("value") # 利用CSS选择器查找
# 改为:
inputTag = driver.find_element(By.CSS_SELECTOR, "value")
案例:
from selenium import webdriver
from selenium.webdriver.common.by import Byoptions = webdriver.EdgeOptions()
options.add_experimental_option('detach', True)driver = webdriver.Edge(options=options)
driver.get('https://layui.dev/docs/2/upload/')
#点击图片上传按钮
driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div[2]/div[2]/div[1]/div[2]/button").click()