2015-12-01 96 views
0
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.common.action_chains import ActionChains 
from selenium.webdriver.common.proxy import * 
import time 

myProxy = "xxx.x.x:yyy" 

proxy = Proxy({ 
    'proxyType': ProxyType.MANUAL, 
    'httpProxy': myProxy, 
    'ftpProxy': myProxy, 
    'sslProxy': myProxy, 
    'noProxy': '' # set this value as desired 
    }) 
driver = webdriver.Firefox(proxy=proxy) 
### 

alert = driver.switch_to_alert() 
time.sleep(1) 
ActionChains(driver).send_keys('xxxxx') 
ActionChains(driver).send_keys(Keys.TAB).perform() 
ActionChains(driver).send_keys('yyyy') 
alert.accept() 

該對話框出現並彈出,但項目只是掛起,不會輸入任何鍵。我如何填寫對話框字段?Python - 爲什麼不是這個硒警報,發送鍵,工作?

回答

0

嘗試使用下面的代碼,而不是ActionChains

alert.send_keys('xxxxx') 
alert.send_keys(Keys.TAB) 
alert.send_keys('yyyy') 

您也可以試試以下以及

alert = driver.switch_to_alert() 
time.sleep(1) 
ActionChains(driver).send_keys('xxxxx').send_keys(Keys.TAB).send_keys('yyyy').perform() 
alert.accept() 

ActionChains將隊列中的動作,當perform被稱爲執行行動

您可以參考以下鏈接ActionChains http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains