1.获取cookie信息
from selenium import webdriver driver=webdriver.Firefox() driver.get("https://www.baidu.com") for cookie in driver.get_cookies(): print(cookie)#获得所有cookie print(driver.get_cookie("PSTM"))#获得指定cookie driver.delete_cookie("PSTM")#删除cookie driver.delete_all_cookies()#删除所有cookie
2.页面等待
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By driver=webdriver.Firefox() driver.get("https://www.baidu.com") driver.implicitly_wait(20)#等待二十秒再抛异常,隐式等待 WebDriverWait(driver,10).until(#等待十秒,显式等待 EC.presence_of_element_located(By.ID,'safasefsd')#不存在的情况 ) driver.find_elements_by_id("sdasdsdsdwqenjas")
3.多页面
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains driver=webdriver.Chrome() driver.get("https://www.baidu.com") driver.execute_script("window.open('https://www.douban.com')")#打开新的标签、 driver.switch_to.window(driver.window_handles[1]) print(driver.current_url)#显示当前driver所在页面 print(driver.page_source)#打印网页内容
4.代理
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains options=webdriver.FirefoxOptions() options.add_argument("--proxy-server=http://125.110.75.250:9000") driver=webdriver.Firefox() driver.get("http://www.httpbin.org/ip")