I can use Selenium to upload a file as
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(r'https://www.example.com')
a = driver.find_element_by_xpath("//input[@type='file']")
a.send_keys(r'C:\abc.jpg')
b = driver.find_element_by_xpath("//output[@id='result']").text
The above code works well.
Now, I want to use Requests to the same job. I tried to search on the web and implement the code. But I cannot get the result.
import requests
from lxml import html
a = requests.get(r'https://www.example.com')
tree = html.fromstring(a.text)
b = tree.xpath("//input[@type='file']")
b.append(r'C:\abc.jpg')#Is it correct?
The html code is as follows:
<div id="test" class="abc def ghi">
<div class="xyz def ghi">
Drag or<br class="def ghi">Click to input file
</div>
<div class="pqr def ghi">
Upload file
</div>
<label id="select-file" for="input" class="def ghi"></label>
<input type="file" id="input" hidden="" class="def ghi">
</div>
There is no form in the code. How to solve the problem?
requests.postyour (encoded) image.requests.post(url, params=params, data=json.dumps(data), headers=headers)anddata = {"eventType": "AAS_PORTAL_START", "data": {"uid": "hfe3hf45huf33545", "aid": "1", "vid": "1"}}. But I don't know how to pass in the required dictionary in my case. Is itdata={"//input[@type='file']": r'C:\abc.jpg' }?requests.post('https://www.example.com', files={b.attrib['name']: open(r'C:\abc.jpg', 'rb')})