如何使用Selenium Python将PDF上传到Google Drive

2024-09-29 23:25:10 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好 我想通过selenium自动上传PDF文件 用Python编写 因为谷歌硬盘不允许仅仅通过发送U键来完成 我希望获得有关如何自动执行此操作的帮助

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium import webdriver as wd


driver=webdriver.Chrome(executable_path="C:\\Users\\l1313\\Desktop\\app\\chromedriver.exe")

p="C:\\Users\\l1313\\Desktop\\upload\\10.pdf"


driver.get("https://script.google.com/macros/s/AKfycbxw2AvTpz4ksUIVPME3bWca5hp89sE3CjraDGJEu2c/dev")

time.sleep(5)

driver.find_element_by_xpath("/html/body/form/input[1]").send_keys(p)

Tags: 文件fromimportpdftimedriverseleniumcommon
1条回答
网友
1楼 · 发布于 2024-09-29 23:25:10

我用的是一个 “应用程序脚本” 创建一个上传文件,但我需要文件自动上传

如果通过Selenium有可能,我想听听

谢谢

通过此代码 从HTML

<base target="_top">   </head>   <body>
<form>
<input type="file" name="imageFile"/>
<input type="button" value="Upload" onclick="google.script.run.upload(this.parentNode)">
</form>   </body> </html>

这是code.gs的代码

function doGet(){
  return HtmlService.createHtmlOutputFromFile("from.html")
  
}

function upload(e){
  //writing the logic to upload the image to drive
  
  var destination_id='1nTxvfpN1L0ZKqCyaFbiCSlcybnwMNteH' //folder id

  var img = e.imageFile

  var contentType = 'image/png'

  var destination = DriveApp.getFolderById(destination_id)
  
  var img = img.getAs(contentType)

  destination.createFile(img)
}

相关问题 更多 >

    热门问题