Selenium库

'''自动化测试工具,支持多种浏览器。爬虫中主要用来解决JavaScrip渲染的问题。'''
'''基本使用'''
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
browser = webdriver.Chrome("D:\ASoft\Python\chromedriver\chromedriver.exe") #声明一个浏览器驱动对象,参数为浏览器驱动的路径。
try:
browser.get("https://www.baidu.com") #跳出浏览器访问IP
input = browser.find_element_by_id("kw") #找到ID为kw的元素赋值给input,即百度输入框
input.send_keys('Python') #输入框输入关键字Python
input.send_keys(Keys.ENTER) #敲回车键
wait = WebDriverWait(browser,10)
wait.until(EC.presence_of_element_located((By.ID,'content_left'))) #等待元素ID为content_left的元素加载出来
print(browser.current_url) #输出浏览器当前的url
print(browser.get_cookies())
print(browser.page_source) #输出网页源代码
finally:
browser.close()

'''声明浏览器对象'''
# browser = webdriver.Chrome()
# browser = webdriver.Firefox()
# browser = webdriver.Edge()
# browser = webdriver.PhantomJS()
# browser = webdriver.Safari()

'''访问网页'''
print("-访问网页-"*20)
browser = webdriver.Chrome("D:\ASoft\Python\chromedriver\chromedriver.exe") #browser.close()执行之后,需再次声明才能使用。
browser.get("https://www.taobao.com")
print(browser.page_source)
browser.close()

'''查找元素'''
print("-查找元素-"*20)
#单个元素
browser = webdriver.Chrome("D:\ASoft\Python\chromedriver\chromedriver.exe")
browser.get("https://www.taobao.com")
input_first = browser.find_element_by_id('q')
input_first2 = browser.find_element(By.ID,'q')
input_second = browser.find_element_by_css_selector('#q')
input_third = browser.find_element_by_xpath('//*[@id="q"]')
print(input_first,input_first2,input_second,input_third)
browser.close()
#类似方法
#find_element_by_name、find_element_by_xpath、find_element_by_link、
#find_element_by_partial_link_text、find_element_by_tag_name、
#find_element_by_class_name、find_element_by_css_selector
#多个元素
browser = webdriver.Chrome("D:\ASoft\Python\chromedriver\chromedriver.exe")
browser.get("https://www.taobao.com")
list = browser.find_elements_by_css_selector('.service-bd li') #返回一个列表
print(list,'\n')
list = browser.find_elements(By.CSS_SELECTOR,'.service-bd li')
print(list,'\n')
browser.close()
#类似方法
#find_element_by_name、find_element_by_xpath、find_element_by_link、
#find_element_by_partial_link_text、find_element_by_tag_name、
#find_element_by_class_name、find_element_by_css_selector

'''元素交互操作:对获取的元素调用交互方法'''
import time
browser = webdriver.Chrome("D:\ASoft\Python\chromedriver\chromedriver.exe")
browser.get("https://www.taobao.com")
input = browser.find_element_by_id('q')
input.send_keys('iPhone')
time.sleep(1) #睡眠1秒
input.clear()
input.send_keys('iPad')
button = browser.find_element_by_class_name("btn-search")
button.click()
#交互动作:将动作附加到动作链中串行执行
from selenium.webdriver import ActionChains
browser = webdriver.Chrome("D:\ASoft\Python\chromedriver\chromedriver.exe")
url = "http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable"
browser.get(url)
browser.switch_to.frame('iframeResult') #根据ID找到网页的frame
source = browser.find_element_by_css_selector("#draggable") #根据ID找到网页的拖拽元素
target = browser.find_element_by_css_selector("#droppable") #根据ID找到网页的被拖拽元素
actions = ActionChains(browser)
actions.drag_and_drop(source,target)
actions.perform()

'''执行JavaScript'''
browser = webdriver.Chrome("D:\ASoft\Python\chromedriver\chromedriver.exe") #声明一个浏览器驱动对象,参数为浏览器驱动的路径。
browser.get("https://www.zhihu.com/explore")
browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')
browser.execute_script('alert("To Button")')

'''获取元素信息'''
#获取属性
browser = webdriver.Chrome("D:\ASoft\Python\chromedriver\chromedriver.exe") #声明一个浏览器驱动对象,参数为浏览器驱动的路径。
url = "https://www.zhihu.com/explore"
browser.get(url)
logo = browser.find_element_by_id('zh-top-link-logo')
print(logo)
print(logo.get_attribute('class')) #得到logo的class属性值
#获取文本值
input = browser.find_element_by_class_name('zu-top-add-question')
print(input.text)
#获取ID、位置、标签名、大小
print(input.id)
print(input.location)
print(input.tag_name)
print(input.size)

'''Frame'''
from selenium.common.exceptions import NoSuchElementException
browser = webdriver.Chrome("D:\ASoft\Python\chromedriver\chromedriver.exe") #声明一个浏览器驱动对象,参数为浏览器驱动的路径。
url = "http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable"
browser.get(url)
browser.switch_to.frame('iframeResult')
source = browser.find_element_by_css_selector('#draggable')
print(source)
try:
logo = browser.find_element_by_class_name('logo')
except NoSuchElementException:
print('NO LOGO')
browser.switch_to.parent_frame()
logo = browser.find_element_by_class_name('logo')
print(logo)
print(logo.text)

'''等待'''
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
#隐式等待:当使用了隐式等待执行测试的时候,如果WebDriver没有在DOM中找到元素,将等待指定时间,然后再找一次,若还找不到则抛出NoSuchElementException,
#换句话说,当查找元素或元素并没有立即出现的时候,隐式等待将等待一段时间再查找DOM,默认的时间是0。
browser = webdriver.Chrome("D:\ASoft\Python\chromedriver\chromedriver.exe") #声明一个浏览器驱动对象,参数为浏览器驱动的路径。
browser.implicitly_wait(10)
browser.get("https://www.zhihu.com/explore")
input = browser.find_element_by_class_name('zu-top-add-question')
print(input)
# 显示等待:在设定等待时间内查找元素,若找到则继续执行其他操作;若到了指定时间找不到则TimeoutException
browser.get("https://www.taobao.com/")
wait = WebDriverWait(browser,1)
input = wait.until(EC.presence_of_element_located((By.ID,'q')))
button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.btn-search')))
print(input,button)
#可以做为判断的有:
# title_is 标题是某内容
# title_contains 标题包含某内容
# presence_of_element_located 元素加载出,传入定位元组,如(By.ID,'p')
# visibility_of_element_located 元素可见,传入定位元组
# visibility_of 可见,传入元素对象
# presence_of_all_elements_located 所有元素加载出
# text_to_be_present_in_element 某个元素文本包含某文字
# text_to_be_present_in_element_value 某个元素值包含某文字
# frame_to_be_available_and_switch_to_it frame加载并切换
# invisibility_of_element_located 元素不可见
# element_to_be_clickable 元素可点击
# staleness_of 判断一个元素是否仍在DOM,可判断页面是否已经刷新
# element_to_be_selected 元素可选择,传元素对象
# element_located_to_be_selected 元素可选择,传入定位元祖
# element_selection_state_to_be 传入元素对象以及状态,想等返回True
# element_located_selection_state_to_be 传入定位元祖以及装填,相等返回True,否则返回False
# alert_is_present 是否出现Alert

'''前进后退'''
import time
browser = webdriver.Chrome("D:\ASoft\Python\chromedriver\chromedriver.exe") #声明一个浏览器驱动对象,参数为浏览器驱动的路径。
browser.get("https://www.baidu.com")
browser.get("https://www.taobao.com")
browser.get("https://www.python.org")
browser.back()
time.sleep(1)
browser.forward()
browser.close()

'''cookies'''
browser = webdriver.Chrome("D:\ASoft\Python\chromedriver\chromedriver.exe") #声明一个浏览器驱动对象,参数为浏览器驱动的路径。
browser.get("https://www.zhihu.com/explore")
print(browser.get_cookies(),'\n')
browser.add_cookie({'name':'name','domain':'www.zhihu.com','value':'germey'})
print(browser.get_cookies(),'\n')
browser.delete_all_cookies()
print(browser.get_cookies())

'''选项卡管理'''
import time
browser = webdriver.Chrome("D:\ASoft\Python\chromedriver\chromedriver.exe") #声明一个浏览器驱动对象,参数为浏览器驱动的路径。
browser.get("https://www.baidu.com/")
browser.execute_script('window.open()')
print(browser.window_handles)
browser.switch_to_window(browser.window_handles[1])
browser.get("https://www.taobao.com")
time.sleep(1)
browser.switch_to_window(browser.window_handles[0])
browser.get("https://python.org")

'''异常处理'''
from selenium.common.exceptions import TimeoutException,NoSuchElementException
browser = webdriver.Chrome("D:\ASoft\Python\chromedriver\chromedriver.exe") #声明一个浏览器驱动对象,参数为浏览器驱动的路径。
try:
browser.get("https://www.baidu.com/")
except TimeoutException:
print('Time Out')
try:
browser.find_element_by_id('hello')
except NoSuchElementException:
print('No Element')
finally:
browser.close()













猜你喜欢

转载自www.cnblogs.com/wisir/p/10029059.html
今日推荐