selenium笔记
selenium 元素定位方法
- 通过id定位元素:find_element_by_id("id_vaule")
- 通过name定位元素:find_element_by_name("name_vaule")
- 通过tag_name定位元素:find_element_by_tag_name("tag_name_vaule")
- 通过class_name定位元素:find_element_by_class_name("class_name")
- 通过css定位元素:find_element_by_css_selector()
- 通过xpath定位元素:find_element_by_xpath("xpath")
- 通过link:find_element_by_link_text("text_vaule")
- 通过find_element_by_partial_link_text()
常用方法
from selenium import webdriver from time import sleep driver = webdriver.Chrome() driver.implicitly_wait(10) #隐形等待 driver.get('https://www.baidu.com/') sleep(1) #以五种定位方式定位到百度首页的搜索输入框 kw_find = driver.find_element_by_id('kw') #kw_find= driver.find_element_by_class_name('s_ipt') #kw_find= driver.find_element_by_name('wd') #kw_find = driver.find_element_by_xpath('//*[@id="kw"]') #kw_find = driver.find_element_by_css_selector('#kw') #id用#kw,class用.s_ipt ,与css的简写方式相同 #send_keys() 是selenium自带的方法,用来输入文本 kw_find.send_keys('selenium') #使用id定位方式定位到搜索按钮 su_find = driver.find_element_by_id('su') #click() 是selenium自带的方法,用来点击定位的元素 su_find.click() sleep(1) driver.quit()
selenium 实战 - 拖动滚动条
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 from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import Select import time import random # 加载xpath插件 chrome_options = webdriver.ChromeOptions() extension_path = 'D:/extension/XPath-Helper_v2.0.2.crx' chrome_options.add_extension(extension_path) browser = webdriver.Chrome(chrome_options=chrome_options) #browser.maximize_window() wait = WebDriverWait(browser, 25) waitPopWindow = WebDriverWait(browser, 25) browser.get("https://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Daps&field-keywords=phone") time.sleep(random.randrange(5, 10, 1)) # 找到 Next Page 按钮, 属于可见元素 # js代码有两种写法,但是对元素的要求不同,focus更为严格 # 第一种方法:focus targetElem = browser.find_element_by_xpath("//a[@id='pagnNextLink']") browser.execute_script("arguments[0].focus();", targetElem) # 第二种方法:scrollIntoView # targetElem = browser.find_element_by_xpath("//a[@id='pagnNextLink']/span[@id='pagnNextString']") # browser.execute_script("arguments[0].scrollIntoView();", targetElem) # 拖动到可见的元素去 print(f"结束拖动滚动条....") time.sleep(random.randrange(5, 10, 1)) browser.quit()
selenium 实战 - 拖动指定距离
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 from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import Select import time import random # 加载xpath插件 chrome_options = webdriver.ChromeOptions() extension_path = 'D:/extension/XPath-Helper_v2.0.2.crx' chrome_options.add_extension(extension_path) browser = webdriver.Chrome(chrome_options=chrome_options) browser.maximize_window() wait = WebDriverWait(browser, 25) waitPopWindow = WebDriverWait(browser, 25) browser.get("https://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Daps&field-keywords=phone") time.sleep(random.randrange(5, 10, 1)) # 指定像素 jsCode = "var q=document.documentElement.scrollTop=100000" browser.execute_script(jsCode) print("拖动滑动条到底部...") time.sleep(random.randrange(5, 10, 1)) browser.quit()
发送 TAB 移动
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 from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import Select import time import random # 加载xpath插件 chrome_options = webdriver.ChromeOptions() extension_path = 'D:/extension/XPath-Helper_v2.0.2.crx' chrome_options.add_extension(extension_path) browser = webdriver.Chrome(chrome_options=chrome_options) browser.maximize_window() wait = WebDriverWait(browser, 25) waitPopWindow = WebDriverWait(browser, 25) browser.get("https://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Daps&field-keywords=phone") time.sleep(random.randrange(5, 10, 1)) # 找到 Next Page 按钮, 属于可见元素 # 指定元素是 超链接 ———— 可以用Tab键切换到 targetElem = browser.find_element_by_xpath("//a[@id='pagnNextLink']") # 这个元素不是超链接,所以无法接收Tab键 # targetElem = browser.find_element_by_xpath("//a[@id='pagnNextLink']/span[@id='pagnNextString']") targetElem.send_keys(Keys.TAB) print(f"结束拖动滚动条....") time.sleep(random.randrange(5, 10, 1)) browser.quit()
参考资料: