Python: basic use of selenium module

1. What is a selenium module?  
- A module for browser-based automation. (By writing code, let the browser complete the operation automation action) 
The selenium module can help us easily obtain dynamically loaded data 2. Selenium usage process: 
- Environment installation 
- Download a browser driver (Google Chrome) 
- Download Path: http://chromedriver.storage.googleapis.com/index.html 
- Correspondence between driver and browser: http://blog.csdn.net/huilan_same/article/details/51896672 
 After downloading, you can called. 3. Steps: 
1. First determine whether the page has dynamically loaded data. 
2. Correspond to the response through the packet capture tool, and do a global search in the data packet. How to get dynamically loaded data? 
1. Selenium can help us easily obtain dynamically loaded data 
2. Perform simulated login (such as Renren, qq simulated login) to facilitate simulated login








 

Example: instantiate an object:

from selenium import webdriver
from lxml import etree
from time import sleep
#实例化一个浏览器对象(传入浏览器的驱动对象
bro = webdriver.Chrome(executable_path='./chromedriver') #括号里跟一个参数,然后返回一个bro

        # - 接下来就可以编写基于浏览器自动化的操作代码了

bro.get('http://125.35.6.84:81/xk/')#让浏览器发起一个指定url对应请求

#获取浏览器当前页面的源码数据(动态)
page_text=bro.page_source    #这是一个属性,返回这个页面的page_text,可以帮我们获取当前页面所对应的源码数据

#解析数据,可以用xpath啦
tree=etree.HTML(page_text)
li_list=tree.xpath('.//ul[@id="gzlist"]/li')   #这个xpath返回的是一个列表,列表存的是ul标签
for li in li_list:
    name=li.xpath('./dl/@title')[0]
    print(name)
    # - 运行完之后可以关闭,但是关闭之前可以让他停留一段时间再关闭,记得导入sleep
sleep(5)  #停留5秒之后再关闭
bro.quit()

4. Selenium has more automation-based operations 
Open Taobao, then enter a product name in the search box, and then click the search box to let him search 
      - initiate a request: get(url) 
      - label positioning: find series methods 
      - label interaction: send_keys('xxx') 
      - execute js program: execute_script('jsCode') 
      - forward, backward: back(),forword() 
      - close the browser: quit()

Guess you like

Origin blog.csdn.net/leowutooo/article/details/124998342