从Python CGI脚本运行Selenium Webdriver [英] Running Selenium webdriver from a Python CGI script

查看:126
本文介绍了从Python CGI脚本运行Selenium Webdriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个使用Selenium webdriver抓取网站的python脚本.现在,我正在尝试使用CGI从网络运行此脚本. 因此,为确保我的CGI服务器正常工作,我尝试了以下方法:

I created a python script that uses Selenium webdriver to scrap a website. Now I'm trying to run this script from the web using CGI. So to ensure that my CGI server is working I tried this :

import cgi
print 'Content-Type: text/html'
print
list_brand = ['VOLVO','FIAT', 'BMW']
print '<h1>TESTING CGI</h1>'
print '<form>'
print '<select>'
for i in range(3):
      print '<option value="' + list_brand[i] + '">'+ list_brand[i] +'</option>'
print '</select>'
print '</form>'

效果很好.现在,当我通过以下脚本将Selenium与CGI结合使用时:

And it worked fine. Now, When I use Selenium with CGI using this script:

import cgitb
import cgi
from selenium import webdriver

print 'Content-Type: text/html'
print
cgitb.enable(display=0, logdir="C:/path/to/log/directory")
path_to_pjs = 'C:path/to/phantomjs-2.1.1-windows/bin/phantomjs.exe'
browser = webdriver.PhantomJS(executable_path = path_to_pjs)
#Reaching to URL
url = 'http://www.website.fr/cl/2/products'
browser.get(url)
div_set = browser.find_elements_by_class_name('productname')
print '<form>'
print '<select>'
for div in div_set:
      print '<option value="' + div.find_element_vy_tag_name('h3').text + '">'+ div.find_element_vy_tag_name('h3').text +'</option>'
print '</select>'
print '</form>'

页面继续加载,但没有响应.知道这是否有可能(我的意思是从cgi脚本运行selenium)还是为什么我的服务器不响应?

the page keeps loading but doesn't respond. Any idea if this is even possible (I mean running selenium from a cgi script) or why my server doesn't respond ?

推荐答案

好吧,我找到了解决问题的方法!原因之一:我没有注意到我在函数div.find_element_by_tag_name中写了vy而不是by. 第二件事是使用Apache服务器.由于某些原因,使用CGIHTTPServer的lite python服务器无法正常工作.因此,我使用XAMPP修改了httpd.conf文件,最后一件事是将路径#!/Python27/python添加到脚本中.

Well, I found the solution for my problem! for one : I didn't pay attention that I wrote vy instead of by in my functions : div.find_element_by_tag_name. And the second thing was using an Apache server. For some reason the lite python server using CGIHTTPServer doesn't work. So I used XAMPP modified the httpd.conf file and the last thing was adding the path #!/Python27/python to the script.

这篇关于从Python CGI脚本运行Selenium Webdriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆