元素定位
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
driver.find_element_by_id("kw")
driver.find_element_by_name("su")
driver.find_element_by_class_name("s_ipt")
driver.find_element_by_tag_name("input")
driver.find_element_by_link_text("hao123")
driver.find_element_by_partial_link_text("hao")
driver.find_element_by_xpath("//input[@id='kw']")
driver.find_element_by_css_selector(".s_ipt")
driver.find_element_by_css_selector("[type='checkbox']")
driver.find_element(By.ID,"kw")
driver.find_element(By.NAME,"wd")
driver.find_element(By.CSS_SELECTOR,"#kw")
driver.find_element("id","kw")
driver.find_element("css selector","#su")
浏览器操作
driver.set_window_size(480,800)
driver.maximize_window()
driver.back()
driver.forward()
driver.refresh()
操作元素
size = driver.find_element_by_id("kw").size
text = driver.find_element_by_id("kw").text
result = driver.find_element_by_id("kw").get_attribute("type")
ture = driver.find_element_by_id("kw").is_displayed()
鼠标、键盘、下拉列表
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
element = driver.find_element_by_css_selector("[autocomplete='asd']")
ActionChains(driver).double_click(element).perform()
ActionChains(driver).context_click(element).perform()
ActionChains(driver).move_to_element(element).perform()
driver.find_element_by_css_selector("#kw").send_keys(Keys.BACK_SPACE)
driver.find_element_by_css_selector("#kw").send_keys(Keys.SPACE)
driver.find_element_by_css_selector("#kw").send_keys(Keys.CONTROL,'a')
driver.find_element_by_css_selector(".s_ipt").send_keys(Keys.ENTER)
element = driver.find_element_by_tag_name("select")
Select(element).select_by_index(0)
Select(element).select_by_value("0")
Select(element).select_by_visible_text("一周")