# 查找文本框输入文本
driver.find_element(By.CLASS_NAME,"nav-search-input").send_keys("i_cecream查找到了")
#查找到之后点击
driver.find_element(By.CLASS_NAME,"nav-search-btn").click()time.sleep(30)
selenium4的解析。
client调用server之后调用browser
browser返回是否错误到server到client
browser到webdriver到client
元素定位
常用的是ID CLASSS
常用css和xpath定位
driver.find_element(By.CLASS_NAME,"channel-link").click()
# 多个classname
for ele in driver.find_elements(By.CLASS_NAME,"channel-link"):print(ele.text)
driver.find_element(By.TAG_NAME,"input").send_keys("学习selenium")# 最大化窗口
driver.maximize_window()
driver.get("https://www.baidu.com")
driver.find_element(By.NAME,"wd").send_keys("根据name查找")# 点击链接
driver.find_element(By.LINK_TEXT,"新闻").click()
# 点击链接
driver.find_element(By.LINK_TEXT,"新闻").click()# 模糊匹配
driver.find_element(By.PARTIAL_LINK_TEXT,"新闻").click()
from selenium import webdriverimport time
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManageroption = webdriver.ChromeOptions()
option.add_argument("start-maximized")driver=webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=option)driver.get("http://www.baidu.com")
driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("selector")
driver.find_element(By.CSS_SELECTOR,"#su").click()
time.sleep(5)
driver.close()
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("selector")
# driver.find_element(By.CSS_SELECTOR,"#su").click()
# driver.find_element(By.CSS_SELECTOR,"[name='wd']").send_keys("selector2342")
# 根据标签属性定位
driver.find_element(By.CSS_SELECTOR,"a[href='https://pan.baidu.com?from=1026962h']").click()# 模糊匹配
driver.find_element(By.CSS_SELECTOR,"a[href*='pan.baidu.com']").click()
# 模糊匹配开头,后面匹配
driver.find_element(By.CSS_SELECTOR,"a[href^='https://pan.baidu.com']").click()
# xpath
driver.find_element(By.XPATH,"//input[@id='kw']").send_keys("ke")driver.find_element(By.XPATH,"//input[starts-width(@title,'text')]").send_keys("ke")# driver.find_element(By.XPATH,"//span[text()='']").click()
# 显示等待 要等待所有加载
#引入WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait
#引入expected_conditions类,并重命名为EC
from selenium.webdriver.support expected_conditions as EC
#设置等待
wait = WebDriverWait(driver,10,0.5)
wait.until(EC.presence_of_element_located((By.ID,'KW')))# 隐式等待
# driver.implicitly_wait(10)
#设置等待
# wait = WebDriverWait(driver,10,0.5)
# wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'.ivu-radio-input')))driver.find_elements(By.XPATH,"//input[@type='radio' and @class='ivu-radio-input']")[1].click()
driver.find_elements(By.XPATH,"//input[@type='radio' and @class='ivu-radio-input']")[2].click()
driver.find_elements(By.XPATH,"//input[@type='radio' and @class='ivu-radio-input']")[3].click()
driver.find_element(By.XPATH, "//span[text()='Android']").click()
time.sleep(2)
driver.find_element(By.XPATH, "//span[text()='Windows']").click()
time.sleep(2)
driver.find_element(By.XPATH,"//span[text()='Android']/preceding-sibling::span/input").click()
time.sleep(5)
driver.find_element(By.XPATH, "//span[text()='香蕉']").click()
select = Select(driver.find_element(By.ID,'s1'))
select.select_by_value("1")
选择
select = Select(driver.find_element(By.ID,'s1'))
select.select_by_index(1)
级联选择
driver.find_element(By.XPATH,"//input[@class='ivu-input ivu-input-default']").click()
driver.find_element(By.XPATH,"//li[contains(text(),'北京')]").click()
driver.find_element(By.XPATH,"//li[contains(text(),'王府井')]").click()
alert
upload= driver.find_element(By.XPATH,'//input[@id="file"]')
upload.send_keys(r"G:\pythonpro\demotest\1")
driver.find_elements(By.XPATH,'//input[@name="submit"]').clear()
进入iframe 切换到iframe和父元素