2017-08-09 161 views
0

我創建了一個使用Selenium webdriver刪除網站的python腳本。現在我試圖使用CGI從Web運行此腳本。 因此,爲了確保我的CGI服務器工作我嘗試這樣做:從Python CGI腳本運行Selenium webdriver

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

它工作得很好。現在,當我用CGI使用Selenium時,使用以下腳本:

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腳本運行硒)或爲什麼我的服務器不響應?

回答

0

嗯,我找到了我的問題的解決方案!第一:我沒有注意到我在我的功能中寫了vy而不是bydiv.find_element_by_tag_name。 第二件事是使用Apache服務器。由於某些原因,使用CGIHTTPServer的lite python服務器不起作用。因此,我使用XAMPP修改了httpd.conf文件,最後一項是將腳本#!/Python27/python添加到腳本中。