2011-10-17 12 views
49

Sono totalmente nuovo per il selenio. Voglio eseguire uno snippet di javascript nel seguente codice (come commentato nel codice), ma non posso farlo. Per favore aiuto.javascript in esecuzione in selenio utilizzando Python

from selenium import webdriver 
import selenium 
from selenium.common.exceptions import NoSuchElementException 
from selenium.webdriver.common.keys import Keys 
import time 

patch = raw_input("Enter patch number\n") 
rel = raw_input("Enter release\n") 
plat = raw_input("Enter port\n") 

browser = webdriver.Firefox() 

browser.get("xxxxxxxxxxxxxxxxx") 

pdtfamily = browser.find_element_by_id("prodFamilyID") 
pdtfamily.send_keys("Database & Tools" + Keys.TAB) 
time.sleep(5) 

pdt = browser.find_element_by_id("productID") 
pdt.send_keys("Intelligent Agent" + Keys.TAB) 
time.sleep(5) 

pdt1 = browser.find_element_by_id("patchCacheChkBxID") 
pdt1.send_keys(Keys.SPACE) 
time.sleep(5) 

pdt7 = browser.find_element_by_id("M__Idf") 
pdt7.send_keys(plat) 

pdt8 = browser.find_element_by_id("M__Idg") 
pdt8.send_keys("American English") 

# Here I want to execute this javascript - "submitForm('patchCacheAdd',1,{'event':'ok'});return false" 

browser.close() 

Se uso - errori

selenium.GetEval("submitForm('patchCacheAdd',1,{'event':'ok'});return false") 

fuori come -

AttributeError: 'module' object has no attribute 'GetEval'I 

risposta

78

Prova browser.execute_script invece di selenium.GetEval.

Vedere this answer per esempio.

30

Usa execute_script, ecco un esempio di pitone:

driver = webdriver.Firefox() 

driver.get("http://stackoverflow.com/questions/7794087/running-javascript-in-selenium-using-python") 
driver.execute_script("document.getElementsByClassName('comment-user')[0].click()") 
+0

Un campione commento per testare il codice di cui sopra –

Problemi correlati