首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Selenium下载google

用Selenium下载google
EN

Stack Overflow用户
提问于 2015-07-16 20:42:43
回答 2查看 2.4K关注 0票数 1

我正在尝试用Python中的Selenium作为pdf下载一个google。不幸的是,我的html知识非常少,因此我不知道我需要什么html让它点击文件,然后下载为pdf。我意识到我可以使用工具来获得html,但这并不适合我。

以下是我到目前为止尝试过的:

代码语言:javascript
复制
from selenium import webdriver 

url = ' https://docs.google.com/document/d/1Y1n-RR5j_FQ9WFMG8E_ajO0OpWLNANRu4lQCxTw9T5g/edit?pli=1' 

browser = webdriver.Firefox()
browser.get(url)

任何帮助都将不胜感激;谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-16 22:04:42

正如你在评论中提到的,Google不喜欢被刮掉。

驾驶命令看起来是适合这类工作的工具。-你想做的事,但不是你想做的方式.根据docs (即我还没有测试它),这个命令看起来像是下载了您的文件:

drive pull --export docx --id 1Y1n-RR5j_FQ9WFMG8E_ajO0OpWLNANRu4lQCxTw9T5g

(总的来说,我发现使用Selenium最简单的方法是使用硒IDE告诉Selenium您想要做什么,然后通过File > Export Test Case As... > Python 2 / unittest / Web Driver导出结果测试用例。)

希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2019-07-20 22:49:43

我有一个可行的解决方案,我不知道谷歌是否会更新以缓解这一状况。现在这是在c#中,但是selenium功能基本上是相同的。显示所有的菜单项,除了下载作为菜单,并返回下载作为webelement。使用selenium单击它,然后选择一个格式并返回要单击的webelement。我无法只使用javascript进行单击,我无法知道如何触发它,但是使用selenium驱动程序单击它效果很好。

使菜单的大部分可见,并返回下载作为webelement。

代码语言:javascript
复制
  document.querySelector(`#docs-file-menu`).className = 'menu-button goog-control goog- 
  inline-block goog-control-open docs-menu-button-open-below';
  document.querySelector(`#docs-file-menu`).setAttribute('aria-expanded', 'true');
  document.querySelectorAll(`.goog-menu:not(.goog-menu-noaccel)`)[0].className = 'goog-menu goog-menu-vertical docs-material docs-menu-hide-mnemonics docs-menu-attached-button-above';
  document.querySelectorAll(`.goog-menu:not(.goog-menu-noaccel)`)[0].setAttribute('style', 'user-select: none; visibility: visible; left: 64px; top: 64px;');
  // download as
  // 2 parents above 
  document.querySelector(`[aria-label='Download as d']`).parentElement.parentElement.className = 'goog-menuitem apps-menuitem goog-submenu goog-submenu-open goog-menuitem-highlight'
  return document.querySelector(`[aria-label='Download as d']`).parentElement.parentElement;

单击下载作为btn:

代码语言:javascript
复制
IWebElement btn = (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript(btnClickJs);
btn.Click();

选择格式:

代码语言:javascript
复制
var formatCss = document.querySelectorAll(`.goog-menu.goog-menu-noaccel`)[6].querySelectorAll(`.goog-menuitem.apps-menuitem`)
                            var format = 'injectformathere' ? 'injectformathere' : '.html'

for (let i = 0; i < formatCss.length; i++) {
   if(formatCss[i].innerText.indexOf(format)!= -1)
       return formatCss[i]    
   }
return null

单击格式:

代码语言:javascript
复制
btn = (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript(btnClickJs);
if (btn != null)
  btn.Click();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31464154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档