如何使用selenium在网页上阅读JSON数据?

时间:2018-03-18 23:42:35

标签: python selenium

我正在尝试从我的网页上读取json文件wifi_build_audit.json,匹配版本(比如说9.130.28.12.32.1.31)并点击“操作”列中的submit按钮,我就能够获得我可以打开并使用密码登录的点,但需要一些帮助,如何阅读json文件并单击提交按钮?

from selenium import webdriver
import selenium
driver = webdriver.Chrome("/Users/username/Downloads/chromedriver")
driver.get("http://wifi-firmware-web.company.com:8080/wifi_build_audit")
username = driver.find_element_by_id('username')
password = driver.find_element_by_id('password')

username.send_keys("username")
password.send_keys("password)

driver.find_element_by_id("login-submit").click()
#Read json from wifi_build_audit.json
#Match the version `9.130.28.12.32.1.31` and click on "submit" in Actions column

我的网页的屏幕截图

enter image description here

UPDATE:
Following are the steps am trying to automate

1.How to read json from wifi_build_audit.json file?
2.Match the version `9.130.28.12.32.1.31` in the JSON and click on "submit" in Actions column on the web page

wifi_build_audit.json
      [{
        "_id": "5aac5fb75071011018001015cff1010c",
        "status": "Completed",
        "endRev": "c7a9aa810159699638d906f5de10195e990699973f1019",
        "chip": "9369",
        "startRev": "d009c101d7fb1016a66e89cd1019b010110110165ad3730e66f6",
        "audit": "Build Audit 9369/goatHW_9369 [9.10130.1018.101101.3101.101.36] \n",
        "sanity": "none",
        "version": "9.10130.1018.101101.3101.101.36",
        "branch": "goatHW_9369",
        "directory": "/SWE/Teams/tech/Furm/submissions/9369/9.10130.1018.101101.3101.101.36",
        "requestTime": "10101018-03-1017T00:101101:1015.907Z",
        "__v": 0,
        "tag": "Official_9.10130.1018.101101.3101.101.36",
        "swe": "baseline2101A10130",
        "projects": [
          {
            "name": "BCMFurm_9369_B101_ekans",
            "_id": "5aac5fb75071011018001015cff1011015",
            "submission": {
              "status": "passed",
              "system": "machine.company.com"
            }
          },
          {
            "name": "BCMFurm_9369_B101_sid",
            "_id": "5aac5fb75071011018001015cff1011019",
            "submission": {
              "status": "passed",
              "system": "machine.company.com"
            }
          },
          {
            "name": "BCMFurm_9369_B101_maui",
            "_id": "5aac5fb75071011018001015cff1011013",
            "submission": {
              "status": "passed",
              "system": "machine.company.com"
            }
          },
          {
            "name": "BCMFurm_9369_B101_kauai",
            "_id": "5aac5fb75071011018001015cff101101101",
            "submission": {
              "status": "passed",
              "system": "machine.company.com"
            }
          },
          {
            "name": "BCMFurm_9369_B101_midway",
            "_id": "5aac5fb75071011018001015cff101101101",
            "submission": {
              "status": "passed",
              "system": "machine.company.com"
            }
          },
          {
            "name": "BCMFurm_9369_B101_nihau",
            "_id": "5aac5fb75071011018001015cff1011010",
            "submission": {
              "status": "passed",
              "system": "machine.company.com"
            }
          },
          {
            "name": "BCMFurm_9369_B101_lanai",
            "_id": "5aac5fb75071011018001015cff1010f",
            "submission": {
              "status": "passed",
              "system": "machine.company.com"
            }
          },
          {
            "name": "BCMFurm_9369_B101_hanauma",
            "_id": "5aac5fb75071011018001015cff1010e",
            "submission": {
              "status": "passed",
              "system": "machine.company.com"
            }
          },
          {
            "name": "BCMFurm_9369_Notes",
            "_id": "5aac5fb75071011018001015cff1010d",
            "submission": {
              "status": "passed",
              "system": "machine.company.com"
            }
          }
        ],
        "approvalNotes": [],
        "stepping": []
      },
      {
        "_id": "5aac638a60eb8c001015ff38cb",
        "status": "Queued",
        "endRev": "99d101c101d79ddbd10110169bc101cfb53f6339e1011019101d799f",
        "chip": "9355",
        "startRev": "6fad101101e9101d9ab81016ee67509101101101e9e9101e101931017f79",
        "audit": "Build Audit 9355/goatHW_9355 [9.10130.1018.101101.3101.101.3101] \n",
        "sanity": "none",
        "version": "9.130.28.12.32.1.31",
        "branch": "goatHW_9355",
        "directory": "/SWE/Teams/tech/Furm/submissions/9355/9.10130.1018.101101.3101.101.3101",
        "requestTime": "10101018-03-1017T00:38:39.1011019Z",
        "__v": 0,
        "tag": "Official_9.10130.1018.101101.3101.101.3101",
        "swe": "baseline101A10199",
        "projects": [
          {
            "name": "BCMFurm_9355_C101_hawaii",
            "_id": "5aac638a60eb8c001015ff38cd",
            "submission": {
              "status": "passed",
              "system": "machine.company.com"
            }
          },
          {
            "name": "BCMFurm_9355_Notes",
            "_id": "5aac638a60eb8c001015ff38cc",
            "submission": {
              "status": "passed",
              "system": "machine.company.com"
            }
          }
        ],
        "approvalNotes": [],
        "stepping": []
      }]

2 个答案:

答案 0 :(得分:0)

您可以尝试在新标签页中打开wifi_build_audit.json的网址,然后从浏览器中读取内容,或使用http客户端库在脚本中发送http请求并从响应中获取内容。

driver.find_element_by_id("login-submit").click()

# open the json file in new tab
json_url = 'http://xxxxxx/wifi_build_audit.json'
driver.execute_script('window.open("'+json_url+'")')

# switch to the new tab
driver.switch_to_window(driver.window_handles[-1])
# read the json content
json_content = driver.find_element_by_css_selector('body > pre').text

# switch to the last window
driver.switch_to_default_content()

答案 1 :(得分:0)

由于您不需要从网页中抓取数据,因为它在json文件中可用 - selenium可能不是正确的解决方案。

您只需找到提交按钮的链接即可。您可以通过浏览器中的页面访问该页面,然后右键单击该链接以查看其来源。

您需要找出点击提交时传递给服务器的参数。

现在你的步骤是:

  1. 下载json文件并阅读。
  2. 搜索所需的版本号
  3. 如果找到,请向网站发送请求(即点击“提交”按钮)
  4. 使用requests;这是完成上述工作的一种方法。

    import requests
    
    username = 'user'
    password = 'sekret'
    
    target_wifi_version = '1.2.3'
    
    login_url = 'http://example.com/login'
    json_url = 'http://example.com/sample.json'
    submit_target = 'http://example.com/submit_results'
    
    submit_form_params = {'wifi_id': ''}
    
    with requests.Session() as s:
       s.post(login_url, data={'username': username, 'password': password})
       results = s.get(json_url).json()
       found = next((i for i in results if i['version'] == target_wifi_version), None)
       if found:
           submit_form_params['wifi_id'] = target_wifi_version
           s.post(submit_url, data=submit_form_params)