mac 下使用 homebrew 安装 chrome
2023-12-26 software mac selenium homebrew chrome chromedriver 11 mins 11 图 3983 字
真是一波三折血泪史。
起因是要装 chromedriver,先看了官方的地址:https://chromedriver.chromium.org/downloads
怎么回事,不能装最新的,只能装114版本?
看样子 homebrew 的版本没有114的,还是用老一点的chrome版本,才能和官方稳定的 chromedriver 搭配干活。
brew info --cask google-chrome
brew install --cask google-chrome@114
完了,homebrew没有这个版本。网上乱七八糟的不放心,还是找比较像官方的:
- https://mrseawave.github.io/chromium-history-page/
- https://mrseawave.github.io/blogs/articles/2021/04/06/download-chromium-history-version/
下载下来发现用不了,无法运行。
算了,直接源码编译:https://chromium.googlesource.com/chromium/src/+refs
我用了114版本:https://chromium.googlesource.com/chromium/src/+/refs/tags/114.0.5735.346
放弃,怎么可能自己编译?!
又认真看了下,120和对应的chromedriver,这不是有吗?https://googlechromelabs.github.io/chrome-for-testing/
下载好了还是无法运行,心累了。
死马当活马医吧,用 homebrew 把 chrome 和 chromedriver 都装完:
试着跑一下 selenium-server 看:
没问题。用代码跑看也是OK的。写了个简单的动态加载另一个python文件的方法,适合debug。
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
from selenium.webdriver.common.action_chains import ActionChains
import time
import os
import subprocess
import random
import importlib
USERNAME=""
PASSWORD=""
WEBSITE="http://abc.com"
driver = webdriver.Chrome()
module_name = "oaread"
oaread_module = importlib.import_module(module_name)
def main():
global WEBSITE
global driver
driver.get(WEBSITE)
print('Enter ' + driver.title)
# time.sleep(random.randint(4,9))
username_xpath = '//input[contains(@id,"userName")]'
password_xpath = '//input[contains(@id,"password")]'
login_button_xpath = '//*[@id="main"]/table/tbody/tr/td/div[2]/div/login-tab/div/div[2]/div[2]/div[1]'
try:
username_elem = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, username_xpath))
)
# 输入用户名
username_elem.send_keys(USERNAME)
# print(username_elem.get_attribute('outerHTML'))
# 等待密码输入框可见
password_elem = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, password_xpath))
)
# 输入密码
password_elem.send_keys(PASSWORD)
# print(password_elem.get_attribute('outerHTML'))
# 添加等待时间,模拟人为操作的速度
time.sleep(1)
# 等待登录按钮可见
login_button_elem = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, login_button_xpath))
)
# 点击登录按钮
login_button_elem.click()
step_enter_oaread(driver)
except Exception as e:
print(e)
# step_oa_read(driver)
monitor_and_execute(module_name+".py", driver)
def get_file_modification_time(file_path):
"""
获取文件的修改时间
"""
return os.path.getmtime(file_path)
def monitor_and_execute(script_path, driver):
"""
监控文件是否有更新,如果有更新则执行脚本
"""
last_modification_time = get_file_modification_time(script_path)
while True:
current_modification_time = get_file_modification_time(script_path)
if current_modification_time > last_modification_time:
print("File has been updated. Executing the script.")
last_modification_time = current_modification_time
importlib.reload(oaread_module)
try:
oaread_module.my_run(driver)
except Exception as e:
print("执行出错,不管了")
print(e)
print("sleep 1")
time.sleep(1)
def step_oa_read(driver):
errCount = 10
while errCount > 0:
try:
oaread_module.step_first_item(driver)
time.sleep(random.randint(2,4))
oaread_module.step_item_replay(driver)
except Exception as e:
errCount -= 1
print(e)
print("执行出错,不管了")
time.sleep(random.randint(10,20))
time.sleep(random.randint(1,2))
oaread_module.my_first_window(driver)
def step_enter_oaread(driver):
driver.get('http://abc.com)
if __name__=="__main__":
errCount = 10
while errCount > 0:
errCount -= 1
main()