webDriver的安装和使用

webDriver的安装

  • 查看谷歌浏览器的版本
  • 去http://npm.taobao.org/mirrors/chromedriver/下载浏览器版本对应的chromeDriver,notes.txt文件中有写到对应版本,注意查看
  • 将下载好的chromeDriver解压好复制到谷歌浏览器安装目录,并未谷歌浏览器添加环境变量
  • 打开pycharm 使用pip下载selenium模块
  • 创建driver时要制定chromedriver的目录
    driver = webdriver.Chrome(executable_path="C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")

webDriver的简单使用

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.support.select import Select
import time


# 打开浏览器,同时打开首页
url = 'https://jc-lab.yscredit.com/'
driver = webdriver.Chrome(executable_path="C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
# driver = webdriver.Chrome()
driver.get(url)


# 通过js改密码登录
js = "document.getElementById('zhanghaoLogin').style.display='block'"
js2 = "document.getElementById('mobileLogin').style.display='none'"
driver.execute_script(js)
driver.execute_script(js2)

# 通过id定位搜索框,同时输入登录用户名密码
driver.find_element_by_id('username').clear()
# xxx是账号和密码
driver.find_element_by_id('username').send_keys('xxx')
driver.find_element_by_id('password').clear()
driver.find_element_by_id('password').send_keys('xxx')
driver.find_element_by_id('login_btn').click()
time.sleep(3)

#进入首页搜索
driver.find_element_by_id('queryInput').send_keys('西藏家家乐购信息技术股份有限公司')
driver.find_element_by_class_name('query-button').click()
print("cccccccccccc")
time.sleep(2)
#点击企业名
#driver.find_element_by_xpath('/html/body/div[1]/div[3]/div/div[3]/div[1]/div[2]/div/span[2]/div[1]/span[1]').click()
driver.find_element_by_css_selector(' body > div.jc-header-and-main2 > div.monitor-content-container > div > div.main-content-right-container > div.monitor-right-section > div:nth-child(2) > div > span:nth-child(2) > div.list-title > span.company-title.pointer.ng-binding').click()
#company-title pointer ng-binding
#company-title.pointer.ng-binding
print("aaaaaaaaaaaaa")
time.sleep(3)
print("bbbbbbbbbbbbb")
cookies = driver.get_cookies()
print(cookies)
handle = driver.current_window_handle
handles = driver.window_handles
for i in handles:
   # driver.switch_to.window(i)
   # break
   print(i)
   print(handle)
   #if i == handle:

   driver.switch_to.window(i)


driver.find_element_by_id('opinionMsg').click()
time.sleep(3)
driver.find_element_by_css_selector('div#opinionMsgNewsdiv div.rel.time-range').click()
time.sleep(2)
driver.find_element_by_css_selector('div#opinionMsgNewsdiv ul.dropdown-menu.change-list-all li:nth-of-type(1) a').click()


# time.sleep(5)
# driver.quit()

你可能感兴趣的:(爬虫)