I'm trying to create a script in python using post http request to upload this pdf file in a webpage. I've tried like the following but unfortunately, the script could not upload the file.
This is the log-in link. Here are the username SmthShift_123 and password 7/B!yzRd8wuK!N2 for your consideration. Now go to this page and click on the last tab Anhang where you will find the upload option.
To let you visualize - this is how that page looks like.
This is my try so far:
import requests
from bs4 import BeautifulSoup
login_url = 'https://jobs.commerzbank.com/index.php?ac=login'
application_link = 'https://jobs.commerzbank.com/index.php?ac=application&jobad_id=30670'
target_link = 'https://jobs.commerzbank.com/index.php?ac=application&page=6'
upload_link = 'https://jobs.commerzbank.com/inc/candidate_attachments.php'
with requests.Session() as s:
s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'
res = s.get(login_url)
sauce = BeautifulSoup(res.text,"lxml")
elem = {i['name']:i.get('value','') for i in sauce.select('input[name]')}
elem['username'] = 'SmthShift_123'
elem['password'] = '7/B!yzRd8wuK!N2'
s.post(login_url,data=elem)
s.get(application_link)
resp = s.get(target_link)
soup = BeautifulSoup(resp.text,"lxml")
payload = {i['name']:i.get('value','') for i in soup.select('input[name]')}
payload['form-control'] = 'Anschreiben'
payload['upload'] = 'Datei hochladen'
payload['save'] = ''
files = {
'searchButton': open('CV.pdf','rb')
}
s.post(upload_link,files=files,data=payload)
When I execute the above script, it neither saves that file nor throws any error.
I also tried like this (using selenium only to do the uploading) but the script can't choose and upload the file either:
s.post(login_url,data=elem)
s.get(application_link)
resp = s.get(target_link)
driver = webdriver.Chrome()
driver.get(resp.url)
driver.delete_all_cookies()
for cookie in s.cookies.items():
driver.add_cookie({"name": cookie[0], "value": cookie[1]})
driver.get(resp.url)
select = Select(WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "select#upload_category"))))
select.select_by_visible_text("Lebenslauf")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#upload_file"))).send_keys("C://Users/WCS/Desktop/CV.pdf")
How can I choose and upload the pdf file using requests?