I am generating a bot to scrape LinkedIn profiles. At this time I am able to log into my account. The next step is to enter a name into the search bar.
I've written this script to get started:
# connect python with webbrowser-chrome
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
import time
from selenium.webdriver.support.ui import WebDriverWait
import pyautogui as pag
def login_to_linkedin(driver):
username = driver.find_element_by_id("session_key")
username.send_keys("xxxxx#gmail.com")
password = driver.find_element_by_id("session_password")
password.send_keys("password")
driver.find_element_by_class_name("sign-in-form__submit-button").click()
def take_a_screenshot(driver):
loc_time = time.localtime()
time_string = time.strftime("%m/%d/%Y", loc_time)
driver.save_screenshot(time_string+"_screenshot.png")
def goto_network_page(driver,network_url):
driver.get(network_url)
def send_requests_to_users(driver):
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.CLASS_NAME, "class name of an element")))
driver.find_element_by_id("global-nav-typeahead")
driver.send_keys("name")
# javaScript = "window.scrollBy(0,4000);"
# driver.execute_script(javaScript)
# n = int(input("Number of requests: "))
# for i in range(0, n):
# pag.click(441, 666)
# print("Done !")
def main():
# url of LinkedIn
url = "http://linkedin.com/"
# path to browser web driver
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(url)
login_to_linkedin(driver)
send_requests_to_users(driver)
take_a_screenshot(driver)
main()
Expected:
the keys for the search should be entered as expected.
Actual:
nothing happens in the search bar.
The new error that I am receiving is:
Traceback (most recent call last):
File "/Users/evangertis/development/mop/source/security-tools/container_scanning/selenium_test.py", line 48, in <module>
main()
File "/Users/evangertis/development/mop/source/security-tools/container_scanning/selenium_test.py", line 45, in main
send_requests_to_users(driver)
File "/Users/evangertis/development/mop/source/security-tools/container_scanning/selenium_test.py", line 33, in send_requests_to_users
driver.find_element(By.CSS_SELECTOR, ".search-global-typeahead button").click()
File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=97.0.4692.71)
Try the following:
search_container = driver.find_element(By.CSS_SELECTOR, '.search-global-typeahead')
# Click on the search button if the input is not in focus
if 'focused' not in search_container.get_attribute('class'):
driver.find_element(By.CSS_SELECTOR, ".search-global-typeahead button").click()
driver.find_element(By.CSS_SELECTOR, "#global-nav-typeahead input").send_keys("name")
Try the following (based on Nic's answer):
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
search_text = 'Name'
search_container = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.search-global-typeahead')))
if 'focused' not in search_container.get_attribute('class'):
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.search-global-typeahead button'))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#global-nav-typeahead input'))).send_keys(search_text)
search_results = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.search-global-typeahead__hit-text')))
if 'focused' not in search_results.get_attribute('class'):
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.search-global-typeahead__hit-text'))).click()
Related
Attempting to create a regression test using Python for Checkout Process. Getting error when attempting to click Continue button. Have tried using Find_Element with Xpath, CSS Selector and Link Text.
import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(ChromeDriverManager().install())
username = 'bmendenhall#sans.org'
password = 'Lloydcole2!'
welcome = 'Welcome'
firstname = 'Bruce'
lastname = 'Parker'
phone = '555-555-5555'
email = 'bmendenhall+regression#sans.org'
wait = WebDriverWait(driver, 10)
driver.get('https://www.sans.org/')
driver.find_element(By.CSS_SELECTOR, "a[aria-label='Log In']").click()
driver.find_element(By.CSS_SELECTOR, "input[id='username']").send_keys(username)
driver.find_element(By.CSS_SELECTOR, "input[id='password']").send_keys(password)
driver.find_element(By.CSS_SELECTOR, "input[id='regularsubmit']").click()
time.sleep(3)
driver.get('https://www.sans.org/')
trainandcert = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "li[aria-label='Train and Certify']")))
ActionChains(driver).move_to_element(trainandcert).perform()
courses = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "li[aria-label='Courses']")))
ActionChains(driver).move_to_element(courses).click().perform()
driver.find_element(By.CSS_SELECTOR, "a[aria-label='Pricing & Training Options']").click()
driver.get('https://www.sans.org/cyber-security-courses/hacker-techniques-incident-handling/#training-and-pricing')
time.sleep(3)
# Need to add item to cart
driver.find_element(By.CSS_SELECTOR, "a[class='course-run-section-button button button--small button--light button--primary']").click()
time.sleep(3)
# Add on Cart
# button = driver.find_element(By.XPATH, "/html/body/div[6]/div[2]/div/div[1]/div/div/c-rckt-cart/div/lightning-layout/slot/lightning-layout-item[2]/slot/c-rckt-order-summary/div/div/div/lightning-button/button")
# driver.execute_script("arguments[0].click();", button)
time.sleep(30)
driver.find_element(By.XPATH, "//button[#title='proceed to checkout']").click()
time.sleep(10)
driver.find_element(By.XPATH, "//*[#id='input-72']").clear()
driver.find_element(By.XPATH, "//*[#id='input-72']").send_keys(firstname)
driver.find_element(By.XPATH, "//*[#id='input-74']").clear()
driver.find_element(By.XPATH, "//*[#id='input-74']").send_keys(lastname)
driver.find_element(By.XPATH, "//*[#id='input-80']").clear()
driver.find_element(By.XPATH, "//*[#id='input-80']").send_keys(phone)
driver.find_element(By.XPATH, "//*[#id='input-84']").clear()
driver.find_element(By.XPATH, "//*[#id='input-84']").send_keys(email)
driver.find_element(By.XPATH, "//*[#id='input-86']").clear()
driver.find_element(By.XPATH, "//*[#id='input-86']").send_keys(email)
#Attempt to Click Continue via XPath
# driver.find_element(By.XPATH, "//*[#title='continue']").click()
#Attmpt to Click Continue via CSS Selector
driver.find_element(By.CSS_SELECTOR, "button[title='continue']").click()
#Attmpt to Click Continue by button Name
# driver.find_element(By.LINK_TEXT, "Continue").click()
time.sleep(30)
driver.close()
Attempting to proceed through the checkout process. Getting stuck on the Attendee Info page. Unable to click the Continue button
Error Message:
Traceback (most recent call last):
File "/Users/BMendenhall/Desktop/Projects/sans-domain-testing/XTAL-403.py", line 69, in <module>
driver.find_element(By.CSS_SELECTOR, "button[title='continue']").click()
File "/Library/Python/3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 857, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/Library/Python/3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 435, in execute
self.error_handler.check_response(response)
File "/Library/Python/3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"button[title='continue']"}
(Session info: chrome=107.0.5304.87)
New to Python.
Creating smoke test for Checkout process.
Am unable to click the Proceed to Checkout button.
On the Add-Ons page of the Checkout process, I am unable to click the Proceed to Checkout button which allows the user to go to the Attendee Info page of Checkout.
I've tried a few different things. Below is the most basic attempt.
Any guidance would be appreciated.
import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(ChromeDriverManager().install())
username = 'bmendenhall#sans.org'
password = 'Lloydcole2!'
wait = WebDriverWait(driver, 10)
driver.get('https://www.sans.org/')
driver.find_element(By.CSS_SELECTOR, "a[aria-label='Log In']").click()
driver.find_element(By.CSS_SELECTOR, "input[id='username']").send_keys(username)
driver.find_element(By.CSS_SELECTOR, "input[id='password']").send_keys(password)
driver.find_element(By.CSS_SELECTOR, "input[id='regularsubmit']").click()
time.sleep(3)
driver.get('https://registration.sans.org/sans/s/cart/0a62J000000ClOsQAK')
time.sleep(3)
driver.find_element(By.CSS_SELECTOR, "button[title='proceed to checkout']").click()
time.sleep(10)
driver.close()
Error Messaging:
Traceback (most recent call last): File "/Users/BMendenhall/Desktop/Projects/sans-domain-testing/AddtoCartcontinue.py", line 28, in <module>
driver.find_element(By.CSS_SELECTOR, "button[title='proceed to checkout']").click() File "/Library/Python/3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 857, in find_element
return self.execute(Command.FIND_ELEMENT, { File "/Library/Python/3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 435, in execute
self.error_handler.check_response(response) File "/Library/Python/3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"button[title='proceed to checkout']"} (Session
I don't know why but CSS_SELECTOR has problem even with button[title] but XPATH works for me
find_element(By.XPATH, "//button[#title='proceed to checkout']")
BTW:
first I tested it with find_elements (with s at the end) and it finds two buttons with 'proceed to checkout' - maybe this makes some problem with CSS.
Below I have given my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
browser = webdriver.Firefox()
browser.maximize_window()
browser.get("http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html")
browser.implicitly_wait(5)
print(browser.title)
source_element = browser.find_element(By.XPATH, "//*[#id='DHTMLgoodies_dragableElement5']")
target_element = browser.find_element(By.XPATH,"//*[#id='box106']")
actions = ActionChains(browser)
actions.drag_and_drop(source_element,target_element).perform()
My output:-
/home/halovivek/PycharmProjects/pythonProject/venv/bin/python /home/halovivek/PycharmProjects/pythonProject/draganddrop.py
Demo 2: Drag and drop
Traceback (most recent call last):
File "/home/halovivek/PycharmProjects/pythonProject/draganddrop.py", line 21, in <module>
actions.drag_and_drop(source_element,target_element).perform()
File "/home/halovivek/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/common/action_chains.py", line 75, in perform
self.w3c_actions.perform()
File "/home/halovivek/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/common/actions/action_builder.py", line 77, in perform
self.driver.execute(Command.W3C_ACTIONS, enc)
File "/home/halovivek/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
self.error_handler.check_response(response)
File "/home/halovivek/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: TypeError: rect is undefined
Stacktrace:
element.getInViewCentrePoint#chrome://remote/content/marionette/element.js:1185:5
getElementCenter#chrome://remote/content/marionette/action.js:1497:22
dispatchPointerMove/<#chrome://remote/content/marionette/action.js:1378:34
dispatchPointerMove#chrome://remote/content/marionette/action.js:1374:10
toEvents/<#chrome://remote/content/marionette/action.js:1145:16
action.dispatchTickActions#chrome://remote/content/marionette/action.js:1055:35
action.dispatch/chainEvents<#chrome://remote/content/marionette/action.js:1023:20
action.dispatch#chrome://remote/content/marionette/action.js:1029:5
performActions#chrome://remote/content/marionette/actors/MarionetteCommandsChild.jsm:459:18
receiveMessage#chrome://remote/content/marionette/actors/MarionetteCommandsChild.jsm:144:31
Process finished with exit code 1
I could not able to drag and drop it.
How to do the Perform action? I have tried many ways but I could not able to find a correct solution.
To drag_and_drop() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using XPATH:
driver.get("http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html")
drag = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(#id, 'box') and text()='Rome']")))
drop = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(#id, 'box') and text()='South Korea']")))
ActionChains(driver).drag_and_drop(drag, drop).perform()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
This works for me
driver.maximize_window()
driver.get('http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html')
time.sleep(5)
drg = driver.find_element(By.XPATH, "//*[#id='dropContent']//div[#class='dragableBox' and #id='box5']")
drp = driver.find_element(By.XPATH, "//*[#id='countries']//div[#class='dragableBoxRight' and #id='box105']")
ActionChains(driver).drag_and_drop(drg, drp).perform()
time.sleep(5)
driver.quit()
Snapshot
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Firefox()
driver.maximize_window()
driver.get('http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html')
time.sleep(5)
driver.get("http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html")
drag = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(#id, 'box') and text()='Rome']")))
drop = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(#id, 'box') and text()='South Korea']")))
ActionChains(driver).drag_and_drop(drag, drop).perform()
'''drg = driver.find_element(By.XPATH, "//*[#id='dropContent']//div[#class='dragableBox' and #id='box5']")
drp = driver.find_element(By.XPATH, "//*[#id='countries']//div[#class='dragableBoxRight' and #id='box105']")
ActionChains(driver).drag_and_drop(drg, drp).perform()
time.sleep(5)'''
#driver.quit()
This is the modified code.
Now its working. Thank you so much for the help.
I'm trying to extract some public information from a government site and need to scroll through the pages by clicking "Próximo", but I'm getting the following error
Traceback (most recent call last):
File "/home/lantri_rafael/codigo/diariosbr/raf/dom-saojoaquimdabarra/coleta_selenium3.py", line 39, in <module>
main()
File "/home/lantri_rafael/codigo/diariosbr/raf/dom-saojoaquimdabarra/coleta_selenium3.py", line 36, in main
pag = page()
File "/home/lantri_rafael/codigo/diariosbr/raf/dom-saojoaquimdabarra/coleta_selenium3.py", line 19, in page
pag.click()
File "/opt/anaconda/envs/env_diariosbr/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/opt/anaconda/envs/env_diariosbr/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/opt/anaconda/envs/env_diariosbr/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/opt/anaconda/envs/env_diariosbr/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: The element reference of [object String] "{\"element-6066-11e4-a52e-4f735466cecf\":\"d099f837-9079-4668-a534-3099fc73a76c\"}" is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed
Below the code and site
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
def page():
pag = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="Pagination"]/a[#class="next"]')))
list_ed = driver.find_elements_by_xpath('//*[#id="jornal"]/li[#class="lista"]')
def info():
for ed in list_ed:
el_text = ed.text
el = ed.find_element_by_xpath('a').get_attribute('href')
print(el_text)
print(el)
info()
pag.click()
sleep(35)
info()
def main():
global driver
driver = webdriver.Firefox()
driver.get("https://www.imprensaoficialmunicipal.com.br/sao_joaquim_da_barra")
driver.implicitly_wait(10)
pag = page()
if __name__ == '__main__':
main()
any suggestions on how to resolve this?
Problem is because click() loads new HTML in browser and you have to use again find_elements_by_xpath to get elements in new HTML. But you use all time the same list with references to elements on first page and it can't find these elements on next pages.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
def info(driver, delay=5):
next_page = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="Pagination"]/a[#class="next"]')))
all_elements = driver.find_elements_by_xpath('//*[#id="jornal"]/li[#class="lista"]')
for item in all_elements:
text = item.text
href = item.find_element_by_xpath('a').get_attribute('href')
print(text)
print('href:', href)
print('---')
next_page.click()
sleep(delay)
def page(driver):
for _ in range(5):
#info(driver)
info(driver, 2)
def main():
driver = webdriver.Firefox()
driver.get("https://www.imprensaoficialmunicipal.com.br/sao_joaquim_da_barra")
driver.implicitly_wait(10)
pag = page(driver)
if __name__ == '__main__':
main()
I've written a script in python using selenium to log in to a website and then go on to the target page in order to upload a pdf file. The script can log in successfully but throws element not interactable error when it comes to upload the pdf file. This is the landing_page in which the script first clicks on the button right next to Your Profile and uses SIM.iqbal_123 and SShift_123 respectively to log in to that site and then uses this target_link to upload that file. To upload that file it is necessary to click on select button first and then cv button. However, the script throws the following error when it is supposed to click on the cv button in order to upload the pdf file.
I've tried with:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
landing_page = 'https://jobs.allianz.com/sap/bc/bsp/sap/zhcmx_erc_ui_ex/desktop.html#/SEARCH/RESULTS/'
target_link = 'https://jobs.allianz.com/sap/bc/bsp/sap/zhcmx_erc_ui_ex/desktop.html#/APPLICATION/57274787/2/'
driver = webdriver.Chrome()
wait = WebDriverWait(driver,30)
driver.get(landing_page)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,".profileContainer > button.trigger"))).click()
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"input[name='alias']"))).send_keys("SIM.iqbal_123")
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"input[name='password']"))).send_keys("SShift_123")
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"button.loginBtn"))).click()
driver.get(target_link)
button = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"button[class*='uploadBtn']")))
driver.execute_script("arguments[0].click();",button)
elem = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"form[class='fileForm'] > label[data-type='12']")))
elem.send_keys("C://Users/WCS/Desktop/CV.pdf")
Error that the script encounters pointing at the last line:
Traceback (most recent call last):
File "C:\Users\WCS\AppData\Local\Programs\Python\Python37-32\keep_it.py", line 22, in <module>
elem.send_keys("C://Users/WCS/Desktop/CV.pdf")
File "C:\Users\WCS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 479, in send_keys
'value': keys_to_typing(value)})
File "C:\Users\WCS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\WCS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\WCS\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=80.0.3987.149)
This is how I tried using requests which could not upload the file either:
import requests
from bs4 import BeautifulSoup
aplication_link = 'https://jobs.allianz.com/sap/opu/odata/hcmx/erc_ui_auth_srv/AttachmentSet?sap-client=100&sap-language=en'
with requests.Session() as s:
s.auth = ("SIM.iqbal_123", "SShift_123")
s.post("https://jobs.allianz.com/sap/hcmx/validate_ea?sap-client=100&sap-language={2}")
r = s.get("https://jobs.allianz.com/sap/opu/odata/hcmx/erc_ui_auth_srv/UserSet('me')?sap-client=100&sap-language=en", headers={'x-csrf-token':'Fetch'})
token = r.headers.get("x-csrf-token")
s.headers["x-csrf-token"] = token
file = open("CV.pdf","rb")
r = s.post(aplication_link,files={"Slug":f"Filename={file}&Title=CV%5FTEST&AttachmentTypeID=12"})
print(r.status_code)
Btw, this is the pdf file in case you wanna test.
How can I upload a pdf file using send_keys or requests?
EDIT:
I've brought about some changes in my existing script which now works for this link visible there as Cover Letter but fails miserably when it goes for this link visible as Documents . They both are almost identical.
Please refer below solution to avoid your exception,
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.common.action_chains import ActionChains
import os
landing_page = 'https://jobs.allianz.com/sap/bc/bsp/sap/zhcmx_erc_ui_ex/desktop.html#/SEARCH/RESULTS/'
target_link = 'https://jobs.allianz.com/sap/bc/bsp/sap/zhcmx_erc_ui_ex/desktop.html#/APPLICATION/57262231/2/'
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
wait = WebDriverWait(driver,30)
driver.get(landing_page)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,".profileContainer > button.trigger"))).click()
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"input[name='alias']"))).send_keys("SIM.iqbal_123")
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"input[name='password']"))).send_keys("SShift_123")
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"button.loginBtn"))).click()
driver.get(target_link)
driver.maximize_window()
button = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"button[class*='uploadBtn']")))
driver.execute_script("arguments[0].click();",button)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
element = wait.until(EC.element_to_be_clickable((By.XPATH,"//label[#class='button uploadType-12-btn']")))
print element.text
webdriver.ActionChains(driver).move_to_element(element).click(element).perform()
webdriver.ActionChains(driver).move_to_element(element).click(element).perform()
absolute_file_path = os.path.abspath("Path of your pdf file")
print absolute_file_path
file_input = driver.find_element_by_id("DOCUMENTS--fileElem")
file_input.send_keys(absolute_file_path)
Output:
Try this script , it upload document on both pages
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
landing_page = 'https://jobs.allianz.com/sap/bc/bsp/sap/zhcmx_erc_ui_ex/desktop.html#/SEARCH/RESULTS/'
first_target_link = 'https://jobs.allianz.com/sap/bc/bsp/sap/zhcmx_erc_ui_ex/desktop.html#/APPLICATION/57274787/1/'
second_target_link = 'https://jobs.allianz.com/sap/bc/bsp/sap/zhcmx_erc_ui_ex/desktop.html#/APPLICATION/57274787/2/'
driver = webdriver.Chrome()
wait = WebDriverWait(driver,30)
driver.get(landing_page)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,".profileContainer > button.trigger"))).click()
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"input[name='alias']"))).send_keys("SIM.iqbal_123")
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"input[name='password']"))).send_keys("SShift_123")
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"button.loginBtn"))).click()
#----------------------------first upload starts from here-----------------------------------
driver.get(first_target_link)
button = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"button[class*='uploadBtn']")))
driver.execute_script("arguments[0].click();",button)
element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"form[class='fileForm'] > label[class$='uploadTypeCoverLetterBtn']")))
driver.execute_script("arguments[0].click();",element)
file_input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"input[id='COVERLETTER--fileElem")))
file_input.send_keys("C://Users/WCS/Desktop/script selenium/CV.pdf")
wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR,".loadingSpinner")))
save_draft = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,".applicationStepsUIWrapper > button.saveDraftBtn")))
driver.execute_script("arguments[0].click();",save_draft)
close = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,".promptWrapper button.closeBtn")))
driver.execute_script("arguments[0].click();",close)
#-------------------------second upload starts from here-------------------------------------
driver.get(second_target_link)
button = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"button[class*='uploadBtn']")))
driver.execute_script("arguments[0].click();",button)
element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"form[class='fileForm'] > label[data-type='12']")))
driver.execute_script("arguments[0].click();",element)
file_input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"input[id='DOCUMENTS--fileElem")))
file_input.send_keys("C://Users/WCS/Desktop/script selenium/CV.pdf")
wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR,".loadingSpinner")))
save_draft = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,".applicationStepsUIWrapper > button.saveDraftBtn")))
driver.execute_script("arguments[0].click();",save_draft)
close = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,".promptWrapper button.closeBtn")))
driver.execute_script("arguments[0].click();",close)