简体   繁体   English

从Python的CGI脚本运行硒的webdriver

[英]Running Selenium webdriver from a Python CGI script

I created a python script that uses Selenium webdriver to scrap a website. 我创建了一个使用Selenium webdriver抓取网站的python脚本。 Now I'm trying to run this script from the web using CGI. 现在,我正在尝试使用CGI从网络运行此脚本。 So to ensure that my CGI server is working I tried this : 因此,为确保我的CGI服务器正常工作,我尝试了以下操作:

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>'

And it worked fine. 而且效果很好。 Now, When I use Selenium with CGI using this script: 现在,当我使用此脚本将Selenium与CGI结合使用时:

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>'

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 ? 知道这是否可能(我是说从cgi脚本运行selenium)还是为什么我的服务器没有响应?

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 . 一:我没注意,我写vy ,而不是by我的功能: div.find_element_by_tag_name And the second thing was using an Apache server. 第二件事是使用Apache服务器。 For some reason the lite python server using CGIHTTPServer doesn't work. 由于某些原因,使用CGIHTTPServer的lite python服务器无法正常工作。 So I used XAMPP modified the httpd.conf file and the last thing was adding the path #!/Python27/python to the script. 所以我用XAMPP修改httpd.conf文件的最后一件事是将路径#!/Python27/python的脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM