实战题目

1、访问:http://www.51job.com
2、输入搜索关键词 “python”, 地区选择 “北京”(注意,如果所在地已经选中其他地区,要去掉)
3、搜索最新发布的职位, 抓取页面信息。 得到如下的格式化信息
Python开发工程师 | 杭州纳帕科技有限公司 | 杭州 | 0.8-1.6万/月 | 04-27
Python高级开发工程师 | 中浙信科技咨询有限公司 | 杭州 | 1-1.5万/月 | 04-27
高级Python开发工程师 | 杭州新思维计算机有限公司 | 杭州-西湖区 | 1-1.5万/月 | 04-27
Selenium系列(16) - Web UI 自动化基础实战(3) - 图1

Selenium系列(16) - Web UI 自动化基础实战(3) - 图2

代码思路

  1. 定位搜索框,输入python
  2. 点击【地区】
  3. 显式等待,定位所有默认已选中的城市
  4. 取消选中它们
  5. 点击【北京】
  6. 点击【确定】
  7. 点击【搜索】
  8. 定位职位列表,除了第一行
  9. 循环职位列表,获取每一行的信息存入列表
  10. 格式化输出

代码(人为测试时的操作步骤)

  1. # !/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. __title__ =
  5. __Time__ = 2020/3/25 17:52
  6. __Author__ = 小菠萝测试笔记
  7. __Blog__ = https://www.cnblogs.com/poloyy/
  8. """
  9. from time import sleep
  10. from selenium import webdriver
  11. from selenium.webdriver.support.wait import WebDriverWait
  12. from selenium.webdriver.common.by import By
  13. # 设置元素等待实例,最多等10秒,每0.5秒查找一次
  14. def wait_element(driver, by_, element_, timeout=10):
  15. element = WebDriverWait(driver=driver, timeout=timeout).until(
  16. lambda x: x.find_element(by=by_, value=element_)
  17. )
  18. return element
  19. # 设置元素等待实例,最多等10秒,每0.5秒查找一次
  20. def wait_elements(driver, by_, element_, timeout=10):
  21. element = WebDriverWait(driver=driver, timeout=timeout).until(
  22. lambda x: x.find_elements(by=by_, value=element_)
  23. )
  24. return element
  25. # 加载驱动
  26. driver = webdriver.Chrome("../resources/chromedriver.exe")
  27. # 打开网站
  28. driver.get("http://www.51job.com")
  29. driver.maximize_window()
  30. # 搜索框
  31. wait_element(driver, By.CSS_SELECTOR, "#kwdselectid").send_keys("python")
  32. # 地区按钮
  33. wait_element(driver, By.CSS_SELECTOR, "#work_position_input").click()
  34. # 热门城市列表
  35. city_lists = wait_elements(driver, By.CSS_SELECTOR, "div#work_position_click_center_right_list_000000 td em.on")
  36. # 选中北京,取消选中其他城市
  37. for city in city_lists:
  38. sleep(1)
  39. city.click()
  40. wait_element(driver, By.CSS_SELECTOR, "em#work_position_click_center_right_list_category_000000_010000").click()
  41. # 确定按钮
  42. driver.find_element_by_css_selector("#work_position_click_bottom_save").click()
  43. # 搜索按钮点击
  44. wait_element(driver, By.CSS_SELECTOR, "div.top_wrap button").click()
  45. # 找到职位列表
  46. lists = wait_elements(driver, By.CSS_SELECTOR, "div#resultList>div.el")[1:]
  47. for data in lists:
  48. spans = [i.text for i in data.find_elements_by_css_selector("span")]
  49. print(" | ".join(spans))
  50. sleep(10)
  51. # 退出浏览器
  52. driver.quit()

转载:https://www.cnblogs.com/poloyy/p/12631987.html