51Testing软件测试论坛

标题: webDriver的安装和使用 [打印本页]

作者: 草帽路飞UU    时间: 2019-1-29 16:44
标题: webDriver的安装和使用
webDriver的安装

  1. <p>driver = webdriver.Chrome(executable_path="C:\Program Files</p><p>(x86)\Google\Chrome\Application\chromedriver.exe")</p>
复制代码


webDriver的简单使用
  1. # -*- coding: utf-8 -*-
  2. from selenium import webdriver
  3. from selenium.webdriver.support.select import Select
  4. import time


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


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

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

  23. #进入首页搜索
  24. driver.find_element_by_id('queryInput').send_keys('西藏家家乐购信息技术股份有限公司')
  25. driver.find_element_by_class_name('query-button').click()
  26. print("cccccccccccc")
  27. time.sleep(2)
  28. #点击企业名
  29. #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()
  30. 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()
  31. #company-title pointer ng-binding
  32. #company-title.pointer.ng-binding
  33. print("aaaaaaaaaaaaa")
  34. time.sleep(3)
  35. print("bbbbbbbbbbbbb")
  36. cookies = driver.get_cookies()
  37. print(cookies)
  38. handle = driver.current_window_handle
  39. handles = driver.window_handles
  40. for i in handles:
  41.    # driver.switch_to.window(i)
  42.    # break
  43.    print(i)
  44.    print(handle)
  45.    #if i == handle:

  46.    driver.switch_to.window(i)


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


  52. # time.sleep(5)
  53. # driver.quit()
复制代码



作者: Miss_love    时间: 2020-12-31 10:49
支持分享




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2