<selenium>selenium基础操作
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait #交互动作 from selenium.webdriver import ActionChains import time #声明浏览器驱动对象 brower = webdriver.PhantomJS() # brower = webdriver.Chrome() #设置窗口大小 brower.set_window_size(1400,900) try: #get访问 brower.get('https://www.baidu.com/') #开启新选项卡(开启新窗口) brower.execute_script('window.open()') print(brower.window_handles) #切换选项卡 brower.switch_to_window(brower.window_handles[1]) #在新开的选项卡搜索淘宝 brower.get('https://www.taobao.com/') brower.switch_to_window(brower.window_handles[0]) #发现ID为kw的元素(输入框) # input1 = brower.find_element_by_id('q') input2 = brower.find_element_by_css_selector('#kw') # input3 = brower.find_element_by_xpath('//*[@id="q"]') #向发现的输入框发送'美女'二字 input2.send_keys('美女') #在输入框敲入Enter键(搜索) #选择id值,点击一次 Button = brower.find_element_by_id('su') Button.click() time.sleep(1) #最长等待时间 wait = WebDriverWait(brower,10) #等待直到content_left元素被加载出来(显示等待) # wait.until(EC.presence_of_element_located(By.ID,'content_left')) #直到直到元素可点击 # wait.until(EC.element_to_be_clickable(By.CSS_SELECTOR,'content_left')) #前进后退 # brower.back() # brower.forward() #打印源码,cookies print(brower.page_source) print(brower.get_cookies()) #加cookie # brower.add_cookie({1:2,'name':'name'}) print(brower.current_url) finally: # pass brower.close()