常用的元素定位:ID,Name,xpath,css 等等
selenuim demo
bash
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
web_driver=webdriver.Firefox()
url="https://www.baidu.com"
web_driver.get(url)
#最多等待5秒,如果在2秒找到了元素 ,会执行下一步
# web_driver.implicitly_wait(5)
#点击 新闻 连接
el=(By.LINK_TEXT,"新闻")
el_select=web_driver.find_element(*el)
#等待元素 最长等待10秒,每2秒找一次
WebDriverWait(web_driver,10,2).until(EC.presence_of_element_located(el))
el_select.click()
time.sleep(3)
#切回第1个tab
web_driver.switch_to.window(web_driver.window_handles[0])
#搜索selenuim
web_driver.find_element(By.ID,"kw").send_keys("selenuim")
web_driver.find_element(By.ID,"su").click()
time.sleep(3)
web_driver.quit()
DrissionPage demo
bash
import time
from DrissionPage import WebPage
from DrissionPage.common import By as DrissionBy
web_page=WebPage('d')
# get()已内置等待加载开始,后无须跟wait.load_start()
web_page.get(url)
#XPATH 获取元素
# loc=(DrissionBy.XPATH,'//*[@id="s-top-left"]/a[1]')
#link_text 获取元素
loc=(DrissionBy.LINK_TEXT,'新闻')
# 等待元素加载 超时时间为5秒
web_page.wait.eles_loaded(loc,timeout=5)
web_page.ele(loc).click()
time.sleep(3)
# DrissionPage 3.x 开始就支持多 tab 对象共存,对象之间互不影响,而且标签页无需激活即可操作。因此不再需要切换标签页。
loc=(DrissionBy.ID,"kw")
#设置文本框值
web_page.ele(loc).input("DrissionPage")
loc=(DrissionBy.ID,"su")
web_page.ele(loc).click()
time.sleep(10)
web_page.quit()