Python:selenium.webdriver 问题(服务器上没有 X)

标签 python webdriver voip

我拼凑了以下脚本来生成远程 Asterisk/Vicidial 服务器上的每日报告。该脚本从报告中获取源代码,执行一些格式化,将结果保存为文本文件,然后通过 smtp 将其发送给我的老板进行审查。我目前正在本地的 cron 作业上运行该脚本并且它运行良好,但是我希望能够在我的 VPS 上运行它;问题是,脚本会弹出一个 Firefox 窗口,而 VPS 没有 X 或任何类型的 GUI,因此,Firefox 将无法打开,并且无法检索数据。

澄清一下,我安装了 Firefox 和所有必需的模块,VPS 基本上与我的本地机器相同,bar X 和 GUI (Debian Lenny)。

如果有人可以就如何修改此脚本以便在没有 X/GUI 的情况下工作提供任何形式的帮助,我们将不胜感激!

谢谢,托比。

import contextlib
import selenium.webdriver as webdriver
import lxml.html as LH
import lxml.html.clean as clean
import csv 
import sys
import smtplib
from email.mime.text import MIMEText
import email.mime.application
import email
import mimetypes
import datetime

date=datetime.date.today()

url="http://myuser:mypass@ipaddress"+ str(date) + "some_other_string"

ignore_tags=('script','noscript','style')
with contextlib.closing(webdriver.Firefox()) as browser:
    browser.get(url)
    content=browser.page_source
    cleaner=clean.Cleaner()
    content=cleaner.clean_html(content)    
    with open('vicidial_data.html','w') as f:
       f.write(content.encode('utf-8'))
    doc=LH.fromstring(content)
    with open('grab_raw.txt','w') as f:
        for elt in doc.iterdescendants():
            if elt.tag in ignore_tags: continue
            text=elt.text or ''
            tail=elt.tail or ''
            words=' '.join((text,tail)).strip()
            if words:
                words=words.encode('utf-8')
                f.write(words+'\n') 

grab=open( 'grab_raw.txt', 'r' )
grab_list=grab.readlines()
grab.close()
del grab_list[0:21]
grab_out=open("Vicidial_Report-"+str(date)+".txt", 'w')
grab_out.writelines(grab_list)
grab_out.close()

msg=email.mime.Multipart.MIMEMultipart()
msg['Subject']='Vicidial call-report' 
msg['From']='me@mycomapny.com'
msg['To']='myboss@mycompany.com'

body = email.mime.Text.MIMEText("Please find attached call-report for " + str(date))
msg.attach(body)

filename= "Vicidial_Report-"+str(date)+".txt"
fp=open(filename,'rb')
att = email.mime.application.MIMEApplication(fp.read(),_subtype="text")
fp.close()
att.add_header('Content-Disposition','attachment',filename=filename)
msg.attach(att)

s = smtplib.SMTP('smtp.gmail.com')
s.starttls()
s.login('mygmaillogin@mycompanydomain.com','mypassword')
s.sendmail('me@mycompanydomain.com',['myboss@mycompanydomain.com', 'someoneelse@mycompanydomain.com', ],     msg.as_string())
s.quit()

最佳答案

Corey Goldberg explains如何使用 pyvirtualdisplay 执行此操作。

但是,您需要能够安装 pyvirtualdisplay、xvfb 和 xserver-xephyr。

import contextlib
import selenium.webdriver as webdriver
display = pyvirtualdisplay.Display(visible = False, size = (800, 600))
display.start()
with contextlib.closing(webdriver.Firefox()) as driver:    
    driver.get('http://www.google.com')
    print driver.title
    # Google
display.stop()

在 Ubuntu/Debian 上,可以安装必要的包

sudo apt-get install python-setuptools
sudo apt-get install xvfb
sudo apt-get install xserver-xephyr
sudo easy_install pyvirtualdisplay

关于Python:selenium.webdriver 问题(服务器上没有 X),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12543861/

相关文章:

python - 使用 pandas 创建 2 个新列并根据日期分配变量

python - 使用 Cygwin 编译并从 Python 调用的 C 程序挂起

python - Python 请求 Session 对象是否在 gevent greenlet 之间共享,线程安全(在 greenlet 之间)?

ruby - 通过 Ruby WebDriver 获取 chrome 控制台日志

android - Samsung Galaxy SII 密码错误问题

ios - 使用 CallKit 显示用于拨出 VoIP 调用的 iOS native 调用 UI

python - 即时将 GSM 编解码器音频从网络传输到扬声器

python-3.x - 如何使用selenium重新连接到webdriver打开的浏览器?

Selenium WebDriverJs 命令

voip - libjingle 和 XEP-0166 等的不兼容性是什么?