小编Jim*_*m B的帖子

带有 chromedriver 的 Selenium 不能通过 cron 启动

在 CentOS7 上以无头模式使用 Selenium 和 Chromedriver 的 Python 脚本在手动调用时运行良好。

options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('no-sandbox')
self.driver = webdriver.Chrome(chrome_options=options)
Run Code Online (Sandbox Code Playgroud)

然而,当用 crontab 启动脚本时,它会在第 4 行(上面)抛出这个异常。底部的完整追溯。

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally (Driver info: chromedriver=2.38.552522
Run Code Online (Sandbox Code Playgroud)

Cron 是用 crontab -e 设置的

* * * * * cd /to/path && /to/path/.virtualenvs/selenium/bin/python /to/path/script.py -t arg1 arg2 > /to/path/log.txt 2>&1
Run Code Online (Sandbox Code Playgroud)

这产生了找不到 chromedriver 之类的错误。然后我在 crontab -e 中添加了以下内容。
1) 使用 bash 而不是 sh,虽然从 sh 手动启动 python 脚本工作正常
2) 指定 chromedriver 的路径

SHELL=/bin/bash
PATH=/usr/local/bin/
Run Code Online (Sandbox Code Playgroud)

我尝试了在网络上找到的不同建议,例如在我的脚本中向 …

python cron selenium selenium-chromedriver centos7

9
推荐指数
2
解决办法
4642
查看次数

Python 脚本中的 SSH 隧道卡住了

我需要使用 SSH 建立隧道才能访问数据库。使用 sshtunnel ( https://github.com/pahaz/sshtunnel/ )在 Python 代码中创建隧道后,脚本挂起,不再执行其他命令。我从提供的代码中看到的最后一个打印是 local_bind_port。之后的一切都被卡住了。我是否误解了一些基本原则?

当我通过 cmd 行手动创建隧道时,db 连接在单独的 python 脚本中工作正常。

系统:Python 3.6.1、sshtunnel 0.1.2、macOS 10.12、服务器为CentOS 7

蟒蛇代码:

import pymysql.cursors
import sshtunnel

with SSHTunnelForwarder(
        ('gateway_host', gateway_port),
        ssh_username='ssh_username',
        ssh_pkey='/path/to/id_rsa',
        ssh_private_key_password='pw',
        remote_bind_address=('remote_bind', 3306),
        local_bind_address=('127.0.0.1', 3306),
        logger=create_logger(loglevel=1)
) as tunnel:
    print('Establishing connection')

    print('tunnel_bindings:', tunnel.tunnel_bindings)
    print('tunnel_is_up:', tunnel.tunnel_is_up)
    print('local_bind_port:', tunnel.local_bind_port)

    connection = pymysql.connect(host='127.0.0.1',
                                 port=3306,
                                 user='db_username',
                                 password='db_password',
                                 db='db_name',
                                 cursorclass=pymysql.cursors.DictCursor)

    try:
        with connection.cursor() as cursor:
            sql = '''SELECT VERSION();'''
            cursor.execute(sql)
            result = cursor.fetchall()
            print(result)
    finally:
        connection.close()

    print('Finish')
Run Code Online (Sandbox Code Playgroud)

日志输出(在最后一行之后它卡住了):

2017-07-26 22:42:37,788| INF |  Srv-3306/1318@sshtunnel …
Run Code Online (Sandbox Code Playgroud)

tunnel paramiko ssh-tunnel python-3.x

6
推荐指数
0
解决办法
948
查看次数

使用 Selenium (Python) 为 Chrome Headless 设置 Accepted-Lang

通过 ChromeOptions 设置 Accepted-Lang 标头适用于常规 Chrome options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})

我正在尝试切换到新的无头 Chrome,但显然此选项在检查 validator.w3.org 上的标头时无效。我可以用其他方式改变它们吗?有谁知道是否会支持此功能?

在 MacOS 上使用 Chrome 60、Chromedriver 2.30、Selenium 3.4.3、Python 3.6.1

使用此代码:

from selenium import webdriver

print('Start')

options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_experimental_option('prefs', {'intl.accept_languages':'en,en_US'})
driver = webdriver.Chrome(chrome_options=options)

driver.get('http://validator.w3.org/i18n-checker/check?uri=google.com#validate-by-uri+')
print('Loaded')

# Check headers in output.html file. Search for 'Request headers'
html_source = driver.page_source
file = open('output.html', 'w')
file.write(html_source)
file.close

driver.implicitly_wait(5)

# Or check headers with select
# WARNING: This fails with 'headless' chrome option!
element = driver.find_element_by_xpath("//code[@class='headers_accept_language_0']").get_attribute('textContent')
print('Element:', element)

driver.close()

print('Finish')
Run Code Online (Sandbox Code Playgroud)

谢谢!

selenium python-3.x selenium-chromedriver http-accept-language google-chrome-headless

5
推荐指数
1
解决办法
983
查看次数

读取 iOS 上文档存储(“存储文档和数据”)设置的值

在 iOS 的设置屏幕中,我可以为支持 iCloud Drive 的应用程序选择“文档存储”设置(类似于移动数据、相机访问选项)。那里写着“存储文档和数据”,有两个选项:iCloud Drive 或在我的手机上,意思是本地。 在此输入图像描述

如何访问我的应用程序中选定的选项?

我的应用程序基于 UIDocumentBrowser,启用了“支持就地打开文档”和“应用程序支持 iTunes 文件共享”。某些应用程序(例如 Apple Mail)仅在按下“共享”按钮时支持“复制到”选项(与“打开方式”相比)。在这种情况下,我想将共享文档复制到我的应用程序中的相应目录,即本地目录或应用程序的 iCloud Drive 目录,具体取决于用户在常规设置中的选择。

现在,我根据 iCloud Drive 是否启用来决定将文档复制到哪里。然而,有可能 iCloud Drive 已启用,但用户选择存储在他的 iPhone 上(本地)。

商店中有一些基于 UIDocumentBrowser 的应用程序并支持此功能。特别是当他们提供创建新文件功能时

谢谢!

iOS11、Xcode 10.1

ios icloud-drive

5
推荐指数
1
解决办法
584
查看次数