import requests
import json
class requests_webdriver:
def init(self):
self.driver = self.chrome_driver()
def chrome_driver(self):
url = 'http://127.0.0.1:4444/wd/hub/session/'
data = json.dumps({
‘desiredCapabilities’:{
‘browserName’:‘chrome’
}
})
res = requests.post(url,data).json()
session = res[‘sessionId’]
driver = url+session
return driver
def get_url(self,url):
base_url = self.driver+'/url'
data = json.dumps({
"url":url
})
requests.post(base_url,data)
def find_element_by_id(self,value):
base_url = self.driver + '/element'
#json.dumps()将字典转换为字符串
data = json.dumps({
"using": 'id',
"value":value
})
res = requests.post(base_url,data).json()
print(res)
if name == ‘main’:
request_driver = requests_webdriver()
request_driver.get_url(‘https://www.imooc.com/user/newlogin/from_url/’)
request_driver.find_element_by_id(‘email’)
代码里 chrome_driver这个方法有sessionId,但下面没有调用这个方法,调用的是find_element_by_id方法,所以sessionId是怎么来的呢,而且始终不太明白为什么要有这个代码 url = ‘http://127.0.0.1:4444/wd/hub/session/’,执行这个selenium-server-standalone-3.9.1.jar,才能运行