python3 selenium+webdriver+chrome

# -*- coding:utf8 -*-
from selenium import  webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
import time

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions  as EC
driver_path = 'E:\\chromedriver.exe'

#浏览器打开和关闭
def wb_01():
    #初始化一个driver,并且指定chromedriver的路径
    driver  = webdriver.Chrome(executable_path=driver_path)


    #请求网页
    driver.get('https://www.baidu.com')

    # 通过page_source获取网页源代码
    print(driver.page_source)

    #driver.close():关闭当前页面。
    #driver.quit():关闭浏览器
    driver.close()


#定位元素
def wb_02():
    driver = webdriver.Chrome(executable_path=driver_path)

    # 请求网页
    driver.get('https://www.baidu.com')

    #1)
    #find_element_by_id
    inputTag = driver.find_element_by_id('kw')
    print(type(inputTag)) #<class 'selenium.webdriver.remote.webelement.WebElement'>
    inputTag.send_keys('python学习')

    #2
    #find_element_by_class

    #3
    #find_element_by_name

    #4
    #find_element_by_tag_name:通过标签名,如div,

    #5
    #find_element_by_xpath

    #6
    #find_element_by_css_selector

    #上面所有的方法都有find_elements_xxx的版本,可以获取多个。


    # 或者以另一个种方式
    # 需要导入By;from selenium.webdriver.common.by import By
    inputTag2 = driver.find_element(By.ID,'kw')
    inputTag2.send_keys('python入门')



# 操作表单元素
def wb_03():
    driver = webdriver.Chrome(executable_path=driver_path)
    driver.get('https://www.baidu.com')

    inputTag = driver.find_element_by_id('kw')
    inputTag.send_keys('python学习')

    #使用clear()可以清理输入框中内容。
    inputTag.clear()

    # 操纵checkbox
    # checkbox常见是:网页填写密码下面的是否记住的的勾选框。
    driver.get('https://www.douban.com/')
    checkbox = driver.find_element_by_id('form_remember')
    checkbox.click() #勾选  .执行两次就是取消勾选。


    #操作select.也就是下拉框。
    # select元素不能直接点击,因为点击后还需要选中元素。
    # selenium提供了一个类:selenium.webdriver.support.ui,Select.
    # 将获取到的元素当成参数传到这个类中,创建这个对象。以后就可以使用这个对象进行选择。

    driver.get('http://www.w3school.com.cn/tiy/t.asp?f=html_dropdownbox')
    selectTag = Select(driver.find_element_by_name('cars'))
    # 可以根据索引选择。
    selectTag.select_by_index(1)
    # 可以根据值做选择。
    selectTag.select_by_value('audi')#标签里面的value属性。
    #可以根据可视文本选择。
    selectTag.select_by_visible_text('Fiat')
    #取消选中的所有选项
    selectTag.deselect_all()


#行为链:有时候在页面中的操作可能要有很多步,那么这时候可以使用鼠标行为链类ActionChains来完成。
#比如现在要求将鼠标移动到某个元素上执行点击事件。
def wb_04():
    driver = webdriver.Chrome(executable_path=driver_path)
    driver.get('https://www.baidu.com')
    inputTag = driver.find_element_by_id('kw')
    submitTag = driver.find_element_by_id('su')

    actions = ActionChains(driver)
    actions.move_to_element(inputTag)
    actions.send_keys_to_element(inputTag,'python')
    actions.move_to_element(submitTag)
    actions.click(submitTag)
    actions.perform() #执行以上操作。

    #还有更多鼠标操作:
    # click_and_hold(element):点击但不松开鼠标
    # context_click(element):右键点击
    # double_click(element):双击


# cookie操作:
#1
#获取所有的cooKie信息
# for cookie in driver_path.get_cookies()
#     print(cookie)
#2
#根据cooKie的key获取value
# value = driver.get_cookie(key)
#3
#删除所有cooKie:
# driver.delete_all_cookies()
#4
#删除特定的cooKie:
# driver.delete_cookie(key)
def wb_05():
    driver = webdriver.Chrome(executable_path=driver_path)
    driver.get('http://www.10jqka.com.cn/')
    # for cookie in driver.get_cookies():
    #     print(cookie)

    print(driver.get_cookie('v'))




#页面等待
# 现在的网页采用ajax技术,需要等待页面加载出来。
# selenium提供了两种方式:隐式等待和显式等待
# 1.隐:调用driver.implicitly_wait.那么在获取不可用的元素之前,会先等待10秒时间。
# 2.显式:显式等待是表名某个条件成立后才执行获取元素等待的操作。也可以在等待的时候指定一个最大的时间。
# 如果超过这个时间那么就抛出一个异常。显式等待应该使用selenium.webdriver.support.excepted_conditions期待条件
# 和selenium.webdriver.support.ui.WebDriverWait来配合完成。
def wb_06():
    driver = webdriver.Chrome(executable_path=driver_path)
    driver.get('http://www.10jqka.com.cn/')

    # driver.implicitly_wait(30)
    # print(driver.get_cookie('v'))


    WebDriverWait(driver,10).until(
        EC.presence_of_element_located((By.ID,'sdaf'))#如果没有这个id等于sdaf的元素就抛出异常。
    )


#切换页面:有时候窗口中有多个子tab页面,这时候需要进行切换。
# selenium提供了一个叫做switch_to_window来进行切换。具体切换到
# 那个页面,可以从driver.window_handles中找到。
def wb_07():
    url_1 = 'https://www.baidu.com'
    url_2 = 'https://www.douban.com'
    driver = webdriver.Chrome(executable_path=driver_path)
    driver.get(url_1)
    driver.execute_script('window.open("%s")'%url_2) #打开一个标签页。
    print(driver.current_url) #https://www.baidu.com/  还是百度。



    # 切换页面:switch_to_window.要先获取窗口句柄。从0开始,
    driver.switch_to.window(driver.window_handles[1])
    print(driver.current_url)  #https://www.douban.com/

#代理ip
def wb_08():
    ip = '60.177.230.210:18118'
    options = webdriver.ChromeOptions()
    options.add_argument('--proxy-server=http://%s'%ip)

    driver = webdriver.Chrome(executable_path=driver_path,chrome_options=options)
    driver.get('http://httpbin.org/ip')



#webElement元素:WebDriver类。
def wb_09():
    driver = webdriver.Chrome(executable_path=driver_path)
    driver.get('http://www.baidu.com')

    submitTag = driver.find_element_by_id('su')
    print(type(submitTag)) #<class 'selenium.webdriver.remote.webelement.WebElement'>
    print(submitTag.get_attribute('value')) #百度一下

if __name__ =="__main__":
    # wb_01()
    # wb_02()
    # wb_03()
    # wb_04()
    # wb_05()
    # wb_06()
    # wb_07()
    # wb_08()
    wb_09()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325481858&siteId=291194637