python - 无法使用 send_keys 或 requests 上传 pdf 文件

标签 python python-3.x selenium selenium-webdriver web-scraping

我使用 selenium 在 python 中编写了一个脚本来登录网站,然后进入目标页面以上传 pdf 文件。该脚本可以成功登录,但在上传 pdf 文件时抛出 element not interable 错误。这是landing_page其中脚本首先单击 Your Profile 旁边的按钮,然后分别使用 SIM.iqbal_123SShift_123 登录该网站,然后然后使用这个 target_link上传该文件。要上传该文件,需要先单击select按钮,然后单击cv按钮。但是,当脚本应该单击 cv 按钮以上传 pdf 文件时,会引发以下错误。

我尝试过:

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")

脚本遇到指向最后一行的错误:

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)

这就是我尝试使用无法上传文件的请求的方法:

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)

顺便说一句,这是pdf文件以防您想测试。

How can I upload a pdf file using send_keys or requests?

编辑:

我对我的 existing script 进行了一些更改现在适用于此 link可以作为求职信看到,但当它用于此 link 时却惨遭失败。可见为 Documents 。它们几乎完全相同。

最佳答案

请引用以下解决方案以避免出现异常,

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)

输出:

enter image description here

关于python - 无法使用 send_keys 或 requests 上传 pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60838550/

相关文章:

python - 如何从 pyInstaller 中删除/排除模块和文件?

python - str.startswith 是如何真正起作用的?

python - 根据列值拆分数据框并导出到不同的 Excel 工作表

windows-7 - Python 3 - "ImportError: No module named …"

java - 如何处理 Selenium 中动态变化的 ID

python - 将 SQL 转换为 pymongo 查询

python - 使用 pydoc 时导入错误

python - 如何将 pandas 数据框转换为其中一个列值用作键的 defaultdict (class, list)?

python - 对于 E2E 测试 : which is better selenium or protractor for following web stack (Angular, Python 和 MongoDB)?

android - 无法执行 dex : Java heap space Java heap space