Selenium是如何操纵浏览器的? (2)

image

Starting ChromeDriver 2.38.552522 开启ChromeDriver 版本号2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb) on port 9515 监听的端口是9515 Only local connections are allowed. ; 只允许本地链接

1.2 构造请求

请求方式 :POST 请求地址 ::9515/session 请求body :{"capabilities": {"firstMatch": [{}], "alwaysMatch": {"browserName": "chrome", "platformName": "any", "goog:chromeOptions": {"extensions": [], "args": []}}}, "desiredCapabilities": {"browserName": "chrome", "version": "", "platform": "ANY", "goog:chromeOptions": {"extensions": [], "args": []}}}

1.3 使用python requests 向 ChromeDriver发送请求

#encoding:utf8 import requests session_url = 'http://localhost:9515/session' session_pars = {"capabilities": {"firstMatch": [{}], \ "alwaysMatch": {"browserName": "chrome",\ "platformName": "any", \ "goog:chromeOptions": {"extensions": [], "args": []}}}, \ "desiredCapabilities": {"browserName": "chrome", \ "version": "", "platform": "ANY", "goog:chromeOptions": {"extensions": [], "args": []}}} r_session = requests.post(session_url,json=session_pars) print(r_session.json()) 此时Chrome浏览器被打开

image

1.4 查看返回结果

{ "sessionId": "b2801b5dc58b15e76d0d3295b04d295c", "status": 0, "value": { "acceptInsecureCerts": false, "acceptSslCerts": false, "applicationCacheEnabled": false, "browserConnectionEnabled": false, "browserName": "chrome", "chrome": { "chromedriverVersion": "2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb)", "userDataDir": "C:\\Users\\lenovo\\AppData\\Local\\Temp\\scoped_dir1792_5142" }, "cssSelectorsEnabled": true, "databaseEnabled": false, "handlesAlerts": true, "hasTouchScreen": false, "javascriptEnabled": true, "locationContextEnabled": true, "mobileEmulationEnabled": false, "nativeEvents": true, "networkConnectionEnabled": false, "pageLoadStrategy": "normal", "platform": "Windows NT", "rotatable": false, "setWindowRect": true, "takesHeapSnapshot": true, "takesScreenshot": true, "unexpectedAlertBehaviour": "", "version": "68.0.3440.106", "webStorageEnabled": true } }


2、打开深圳-逸遥的博客园

2.1 构造请求

请求方式 :POST 请求地址 ::9515/session/:sessionId/url 注意: 上述地址中的 ":sessionId" 要用启动浏览器的请求返回结果中的sessionId的值 例如:我刚刚发送请求,启动浏览器,返回结果中"sessionId": "b2801b5dc58b15e76d0d3295b04d295c" 然后我构造 导航到"深圳-逸遥的博客园"的请求地址 请求地址::9515/session/b2801b5dc58b15e76d0d3295b04d295c/url 请求body :{"url": "https://www.cnblogs.com/snailrunning", "sessionId": "b2801b5dc58b15e76d0d3295b04d295c"}

2.2 使用python requests 向 ChromeDriver发送请求

#encoding:utf8 import requests url = 'http://localhost:9515/session/b2801b5dc58b15e76d0d3295b04d295c/url' pars = {"url": "https://www.cnblogs.com/snailrunning", "sessionId": "b2801b5dc58b15e76d0d3295b04d295c"} r = requests.post(url,json=pars) print(r.json()) 浏览器打开”深圳-逸遥“的博客园

image

2.3 查看请求返回结果

{'sessionId': 'b2801b5dc58b15e76d0d3295b04d295c', 'status': 0, 'value': None}


3、定位”深圳-逸遥“第一篇博文的标题

3.1 构造请求

请求方式 :POST 请求地址 ::9515/session/:sessionId/element 注意: 上述地址中的 ":sessionId" 要用启动浏览器的请求返回结果中的sessionId的值 例如:我刚刚发送请求,启动浏览器,返回结果中"sessionId": "b2801b5dc58b15e76d0d3295b04d295c" 然后我构造 查找页面元素的请求地址 请求地址::9515/session/b2801b5dc58b15e76d0d3295b04d295c/element 请求body :{"using": "css selector", "value": ".postTitle a", "sessionId": "b2801b5dc58b15e76d0d3295b04d295c"}

3.2 使用python requests 向 ChromeDriver发送请求

#encoding:utf8 import requests url = 'http://localhost:9515/session/b2801b5dc58b15e76d0d3295b04d295c/element' pars = {"using": "css selector", "value": ".postTitle a", "sessionId": "b2801b5dc58b15e76d0d3295b04d295c"} r = requests.post(url,json=pars) print(r.json())

3.3 查看请求返回的结果

{'sessionId': 'b2801b5dc58b15e76d0d3295b04d295c', 'status': 0, 'value': {'ELEMENT': '0.11402119390850629-1'}}

返回结果中的{'ELEMENT': '0.11402119390850629-1'}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zydyxj.html