- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从 http://www.oracle.com/technetwork/server-storage/developerstudio/downloads/index.html 下载文件在 headless 的情况下。我有一个帐户(它们是免费的),但该网站确实并不容易,显然它使用了一系列 javascript 表单/重定向。使用 Firefox,我可以使用元素检查器,在下载开始时将文件的 url 复制为 cURL,并在 headless 机器中使用它来下载文件,但到目前为止,我所有仅在 headless 机器中获取文件的尝试都没有失败。
我已经成功登录:
#!/usr/bin/env python3
username="<my username>"
password="<my password>"
import requests
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.PHANTOMJS
caps["phantomjs.page.settings.userAgent"] = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0"
driver = webdriver.PhantomJS("/usr/local/bin/phantomjs")
driver.set_window_size(1120, 550)
driver.get("http://www.oracle.com/technetwork/server-storage/developerstudio/downloads/index.html")
print("loaded")
driver.find_element_by_name("agreement").click()
print("clicked agreement")
driver.find_element_by_partial_link_text("RPM installer").click()
print("clicked link")
driver.find_element_by_id("sso_username").send_keys(username)
driver.find_element_by_id("ssopassword").send_keys(password)
driver.find_element_by_xpath("//input[contains(@title,'Please click here to sign in')]").click()
print("submitted")
print(driver.get_cookies())
print(driver.current_url)
print(driver.page_source)
driver.quit()
我怀疑登录有效,因为在 cookie 中我看到了一些与我的用户名相关的数据,但在 Firefox 中提交表单会导致在 3-4 次重定向后开始下载,而在这里我什么也没得到,并且 page_source
和 current_url
仍然属于登录页面。
也许该网站正在积极阻止这种使用,或者也许我做错了什么。知道如何实际下载该文件吗?
最佳答案
感谢 TheChetan 的评论,我让它工作了。不过,我没有使用 javascript-blob 路由,而是使用了 Tarun Lalwani 在 https://stackoverflow.com/a/46027215 中建议的 requests
方法。 。我花了一段时间才意识到我也必须修改请求中的用户代理。最后这对我有用:
#!/usr/bin/env python3
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from requests import Session
from urllib.parse import urlparse
from os.path import basename
from hashlib import sha256
import sys
index_url = "http://www.oracle.com/technetwork/server-storage/developerstudio/downloads/index.html"
link_text = "RPM installer"
username="<my username>"
password="<my password>"
user_agent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0"
# set up browser
caps = DesiredCapabilities.PHANTOMJS
caps["phantomjs.page.settings.userAgent"] = user_agent
driver = webdriver.PhantomJS("/usr/local/bin/phantomjs")
driver.set_window_size(800,600)
# load index page and click through
driver.get(index_url)
print("loaded")
driver.find_element_by_name("agreement").click()
print("clicked agreement")
link = driver.find_element_by_partial_link_text(link_text)
sha = driver.find_element_by_xpath("//*[contains(text(), '{0}')]/following::*[contains(text(), 'sum:')]/following-sibling::*".format(link_text)).text
file_url = link.get_attribute("href")
filename = basename(urlparse(file_url).path)
print("filename: {0}".format(filename))
print("checksum: {0}".format(sha))
link.click()
print("clicked link")
driver.find_element_by_id("sso_username").send_keys(username)
driver.find_element_by_id("ssopassword").send_keys(password)
driver.find_element_by_xpath("//input[contains(@title,'Please click here to sign in')]").click()
print("submitted")
# we should be logged in now
def progressBar(title, value, endvalue, bar_length=60):
percent = float(value) / endvalue
arrow = '-' * int(round(percent * bar_length)-1) + '>'
spaces = ' ' * (bar_length - len(arrow))
sys.stdout.write("\r{0}: [{1}] {2}%".format(title, arrow + spaces, int(round(percent * 100))))
sys.stdout.flush()
# transfer the cookies to a new session and request the file
session = Session()
session.headers = {"user-agent": user_agent}
for cookie in driver.get_cookies():
session.cookies.set(cookie["name"], cookie["value"])
driver.quit()
r = session.get(file_url, stream=True)
# now we should have gotten the url with param
new_url = r.url
print("final url {0}".format(new_url))
r = session.get(new_url, stream=True)
print("requested")
length = int(r.headers['Content-Length'])
title = "Downloading ({0})".format(length)
sha_file = sha256()
chunk_size = 2048
done = 0
with open(filename, "wb") as f:
for chunk in r.iter_content(chunk_size):
f.write(chunk)
sha_file.update(chunk)
done = done+len(chunk)
progressBar(title, done, length)
print()
# check integrity
if (sha_file.hexdigest() == sha):
print("checksums match")
sys.exit(0)
else:
print("checksums do NOT match!")
sys.exit(1)
所以最后的想法是使用 selenium+phantomjs 进行登录,然后使用 cookie 进行普通请求。
关于javascript - 使用 selenium 进行 headless javascript 下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46222586/
我正在尝试使用 firefox headless 在 headless (headless) redhat linux 构建机器上运行 selenium 测试。我创建驱动程序的方法如下所示: priv
关于这个主题有很多东西可以找到,但无法弄清楚。我需要滚动到(不太长)无限滚动页面的末尾。我有 2 个选项可以使用 chrome 非 headless (headless)但似乎不能 headless
我在远程服务器上运行 OpenFOAM,基本上设法通过 paraview 的 pvserver 可视化结果 as described here .然而,在连接后,客户端产生 Server DISPLA
我想在 headless 模式下截取 Android 设备的屏幕截图,也就是说我是这样创建的: echo no | /opt/android/android-sdk-linux/tools/andro
主要区别在于,基于GUI和非GUI(Headless)执行。 我正在寻找所有Headless浏览器之间的差异,但是很遗憾,我没有找到任何差异。我一个接一个地经历,这使我更加困惑。如果有人可以分享具有差
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭10 年前。 Improve th
我需要在没有 X 服务器的情况下执行 java 图像裁剪和调整大小。 我尝试了几种方法。下面的第一种方法有效,但输出了一个相当难看的调整大小的图像(可能使用最近邻算法来调整大小: static Buf
过去几天我一直在使用 Selenium、Tor 和 Firefox 作为多个任务的组合。我已经设法用 Python 编写了一个简单的脚本,它通过 Selenium 控制 Firefox,而 Firef
我正在使用 pygame 的操纵杆 api 在 headless (headless)系统上对我的项目使用操纵杆,但是 pygame 需要一个“屏幕”,所以我设置了一个虚拟视频系统来克服这个问题。它工
我想使用 headless firefox 在 macos 上捕获网页的图像。 这是我执行的命令:/Applications/Firefox.app/Contents/MacOS/firefox-bi
我正在使用带有 headless-chromium-php 的 google chrome headless (headless)浏览器 导航到某些网站,但它总是被验证码检测到 我尝试使用此 plug
是否有可能使用 Octave headless。 像这样的东西 octave result.txt 最佳答案 使用 octave --silent --eval 5+4 > result.txt 你会
我目前正在尝试在 headless (headless)模式下运行应用程序,我定义了后台回调: void callbackInBackground() { // Invoked from the s
我正在使用LibGDX headless backend运行jUnit测试。这在某些测试中效果很好,但是如果我尝试创建new Texture('myTexture.png');,则会收到NullPoi
我想在这个页面上使用 Selenium:https://www.avis.com/en/home 如果没有 headless (headless)模式,该代码一切正常: import requests
在Jasmine headless (headless)Webkit中运行测试时,我遇到了一个简单的TypeError: 'undefined' is not an object失败。但是没有提示在哪
我负责测试一个大量使用 AJAX 的企业 Web 应用程序。我需要构建一个系统,允许在没有人工干预的情况下连续运行测试。目前我最感兴趣的是负载测试,但我希望用于生成负载的相同脚本用于功能测试。 目前用
TL; DR:我可以配置一个容器来原生访问VGA,以覆盖主机视频输出吗? 我正在考虑处置低功耗的XenServer(以前为ESXi)白盒以设置docker最小安装(例如CoreOS,RancherOs
我正在尝试 headless (headless)运行我的测试,并将我的两个测试套件分片以并行运行它们。在我的本地计算机上,它们并行运行,但在这种 headless (headless)设置中,它们一
仍在尝试为大型大学项目(RCP 产品)建立 headless (headless)构建。 每个 Eclipse 用户都知道以下手动功能:“文件 --> 导入 --> 将现有项目导入工作区”以及“构建工
我是一名优秀的程序员,十分优秀!