动态渲染
warning:
这篇文章距离上次修改已过765天,其中的内容可能已经有所变动。
import time
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
'''基本使用'''
#声明浏览器对象
browser=webdriver.Chrome()
try:
'''访问页面'''
browser.get('https://www.baidu.com')
input=browser.find_element(By.ID,'kw')
input.send_keys('python')
input.send_keys(Keys.ENTER)
wait=WebDriverWait(browser,10)
wait.until(EC.presence_of_element_located((By.ID,'content_left')))
print('='*50)
print(browser.current_url)
print('=' * 50)
print(browser.get_cookies())
print('=' * 50)
# print(browser.page_source)
browser.quit()
except:
browser.close()
'''节点交互'''
driver=webdriver.Chrome()
try:
driver.get('https://www.taobao.com')
input=driver.find_element(By.ID,'q')
input.send_keys('手机')
# input.clear() #清除
button=driver.find_element(By.CLASS_NAME,'btn-search')
button.click()
driver.close()
except:
driver.close()
'''动作链(拖拉)'''
driver=webdriver.Chrome()
try:
driver.get('https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
driver.switch_to.frame('iframeResult') #切换frame
source=driver.find_element(By.ID,'draggable')
target=driver.find_element(By.ID,'droppable')
actions=ActionChains(driver)
actions.drag_and_drop(source,target)
actions.perform()
except:
driver.close()
'''执行JavaScript,比如下拉进度条'''
driver=webdriver.Chrome()
try:
driver.get('https://www.zhihu.com/explore')
driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
driver.execute_script('alert("To Bottom")')
except:
print('error')
driver.close()
'''获取属性'''
driver=webdriver.Chrome()
try:
driver.get('https://www.zhihu.com/explore')
logo=driver.find_element(By.ID,'special')
print(logo)
print(logo.get_attribute('class'))
driver.close()
except:
print('error')
driver.close()
'''获取文本'''
driver=webdriver.Chrome()
try:
driver.get('https://www.zhihu.com/explore')
res=driver.find_element(By.CLASS_NAME,'ExploreSpecialCard-contentTitle')
print(res.text)
driver.close()
except:
print('error')
driver.close()
'''获取id、位置、标签吗和大小'''
driver=webdriver.Chrome()
try:
driver.get('https://www.zhihu.com/explore')
res=driver.find_element(By.CLASS_NAME,'ExploreSpecialCard-contentTitle')
print('id',res.id)
print('位置',res.location)
print('标签',res.tag_name)
print('大小',res.size)
driver.close()
except:
print('error')
driver.close()
'''延时显式等待'''
driver=webdriver.Chrome()
try:
driver.get('https://www.taobao.com')
#引入WebDriverWait对象,设置最长等待时间
wait=WebDriverWait(driver,10)
#调用until方法,传入等待条件;EC.presence_of_element_located代表节点出现
input=wait.until(EC.presence_of_element_located((By.ID,'q'))) #直到 id='q'的结点加载出来
button=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.btn-search'))) #直到节点可点击
print(input,button)
driver.close()
except:
print('error')
driver.close()
'''前进和后退'''
driver=webdriver.Chrome()
try:
driver.get('https://www.taobao.com/')
driver.get('https://www.baidu.com/')
driver.get('https://www.python.org/')
driver.back() #后退
time.sleep(1)
driver.forward() #前进
driver.close()
except:
print('error')
driver.close()