编程语言
首页 > 编程语言> > Python:selenium.webdriver问题(服务器上没有X)

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

作者:互联网

我拼凑了以下脚本,以在远程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

标签:webdriver,voip,python
来源: https://codeday.me/bug/20191031/1978940.html