【问题标题】:browser for Python Selenium - or other remote control library - without manual download requirementsPython Selenium 浏览器 - 或其他远程控制库 - 无需手动下载
【发布时间】:2020-01-05 18:32:54
【问题描述】:

我的代码库的一小部分需要自动控制浏览器。我尝试过使用 Requests 和 BeautifulSoup - 我已在其他项目中成功使用过 - 但我遗漏了一些东西并从服务器取回垃圾。

我愿意将 Selenium 用于我需要的东西(或其他一些解决方案)。我设法让它工作,但我发现它需要手动下载驱动程序(我认为)。对于使用我的库的其他人,我想要一些可以自动安装的东西或一些不需要安装的默认选项。

因此,我的问题是,是否有远程控制 Windows 浏览器的选项,它只适用于一个或两个 pip 调用?我更喜欢 Selenium 解决方案,但似乎 Selenium 中的所有浏览器现在都需要额外的手动下载。这是正确的还是我错过了什么?

编辑:我在 Selenium 存储库中添加了自动安装驱动程序作为问题: https://github.com/SeleniumHQ/selenium/issues/7922

【问题讨论】:

  • 我没有看到,谢谢。对于我所需要的 - 只需 3 页自动化 - 一些默认浏览器会更可取,但也许该选项不存在。我还可以直接看到对 Selenium 的更新,它基本上做了 webdriver-manager 所做的事情——假设 Selenium 中尚不存在该功能。
  • 你检查了this。检查这个问题中的 powershelll 选项。
  • @supputuri 感谢您的意见。目标是拥有一个仅通过普通 python 机制(pip、conda 等)工作的库
  • 我确信我们可以使用 pip 安装库,但在这种情况下,您必须下载一个 zip,然后将 .exe 从 zip 中解压缩到所需位置。我可以发布我为在 Ruby 中实现这一目标而开发的逻辑,这可能会给你一个想法。

标签: python windows selenium


【解决方案1】:

Python

编辑1:

您可以使用webdriver_manager 来处理这种情况,它也会处理os.chomp。这是webdriver_manager的pip安装

pip install webdriver-manager

下面是 chrome 的示例脚本。

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.google.com")
driver.quit

旧答案:

================================================ ============== 这将确保 chromedriver 始终是最新的稳定版本,并且您无需执行任何手动步骤

webdriver_manager 的另一个优势是您可以即时下载任何驱动程序。下面是 Firefox (GeckoDriver) 的简单示例。

from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(executable_path =GeckoDriverManager().install())
driver.get("https://www.google.com")
driver.quit()

这是为 windows 动态下载最新 chromedriver 的脏代码。

import requests
import wget
import zipfile
import os

# get the latest chrome driver version number
url = 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE'
response = requests.get(url)
version_number = response.text

# build the donwload url
download_url = "https://chromedriver.storage.googleapis.com/" + version_number +"/chromedriver_win32.zip"

# download the zip file using the url built above
latest_driver_zip = wget.download(download_url,'chromedriver.zip')

# extract the zip file
with zipfile.ZipFile(latest_driver_zip, 'r') as zip_ref:
    zip_ref.extractall() # you can specify the destination folder path here
# delete the zip file downloaded above
os.remove(latest_driver_zip)

RUBY这是在 ruby​​ for windows 中实现的解决方案。

require 'net/http'
require 'open-uri'
require 'zip'

# Method to extract the contents of the zip file to the destination path
def extract_zip(file, destination)
  # create the destination folder if it's not exist
  FileUtils.mkdir_p(destination)
  Zip::File.open(file) do |zip_file|
    zip_file.each do |f|
      fpath = File.join(destination, f.name)
      zip_file.extract(f, fpath) unless File.exist?(fpath)
    end
  end
end

# url where you can get the latest chrome driver information
url = 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE'

# get the latest driver version
parsed_url = URI.parse(url)
http = Net::HTTP.new(parsed_url.host, parsed_url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = Net::HTTP::Get.new(url)
request["Accept"] = 'application/json'
response = http.request(request)

# build the download url based on the above step
download_url = "https://chromedriver.storage.googleapis.com/#{response.body}/chromedriver_win32.zip"
puts download_url

# build the temporary location where you want to store the donwload zip
download_path = File.join(ENV['TEMP'],response.body.gsub('.',"_")) + '.zip'
puts download_path

# download the zip file
File.open(download_path, "wb") do |file|
  file.write open(download_url).read
end

# Extract the chromedriver.exe from the zip in specific location
extract_zip(download_path,"my_destination_folder_path") # don't specify the filename.

# delete the zip file
FileUtils.rm_rf(download_path)

这个 .rb 文件将在我收到驱动程序不支持 chrome 版本异常的错误时执行,并在下载 chromedriver 后恢复执行。这样,我的脚本就不会因为 chrome 版本的变化而失败。

【讨论】:

  • 谢谢,我仍然需要研究并尝试一下,但有机会我会回来评论/接受。
  • 感谢您的建议回答。当然,我第一次去测试它是在 Mac 上!然后显然我使用的是 Chrome 79,而最新版本是 Chrome 80,所以我无法运行它,我不得不手动指定要使用的版本。我还需要运行os.chmod 以授予文件执行所需的权限并添加wget,此时只需下载管理器即可。
  • @Jimbo 请使用webdriver_manager 用法检查更新的答案。如果您有任何问题,请告诉我。
  • 是的,新的答案可能更多的是人们正在寻找的。旧的答案需要比我正在寻找的更多的工作,我认为在版本控制方面遇到了问题(本地 Chrome 与下载的驱动程序)
  • 很高兴我们终于成功了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
  • 2018-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-11
  • 2020-10-12
相关资源
最近更新 更多