2017-08-06 39 views
2

我想在Windows中使用Selenium網絡驅動程序和Python編寫一個字符串到<input>元素。Python的硒找到輸入的元素,但不能send_keys

它的位置和選擇的元素,但webdriver嘗試send_keys("mystring")時失敗。

campotx = driver.find_element_by_css_selector("input.urEdf2TxtRadius.urEdf2TxtEnbl.urEdfVAlign") 
campotx.click() 
campotx.clear() 
##########----it works ok until here 
campotx.send_keys("SM37") 
campotx.send_keys(Keys.ENTER) 

該問題與「查找元素」方法無關:我嘗試了by_ID,by_class_name和by_xpath。他們都能夠找到元素。我只能不能send_keys

我正在使用HTMLUNITS,無頭的webdriver。此問題的輸出誤差是罕見的,我試圖googleing,但無法找到任何東西:

selenium.common.exceptions.WebDriverException: Message: TypeError: Cannot call method "setOpenerRef" of null (http://myurl.com) 

這裏是<input>源代碼:

<input id="ToolbarOkCode" ct="I" lsdata="{0:'ToolbarOkCode',1:'Command',4:200,13:'150px',23:true}" lsevents="{Change:[{ClientAction:'none'},{type:'TOOLBARINPUTFIELD'}],Enter:[{ClientAction:'submit',PrepareScript:'return\x20its.XControlSubmit\x28\x29\x3b',ResponseData:'delta',TransportMethod:'partial'},{Submit:'X',type:'TOOLBARINPUTFIELD'}]}" type="text" maxlength="200" tabindex="0" ti="0" title="Command" class="urEdf2TxtRadius urEdf2TxtEnbl urEdfVAlign" value="" autocomplete="on" autocorrect="off" name="ToolbarOkCode" style="width:150px;"/> 

編輯:

  • 試圖使用PhantomJS,但我無法登錄。
  • 試圖使用headless_ie_selenium.exe,但我甚至無法啓動驅動程序。
  • 試圖使用無頭鍍鉻,與headless_ie_selenium相同的問題。所有的教程都與Linux環境有關。
+2

你能分享到相關頁面或HTML代碼嗎? –

+1

而不是HTMLUNITS,請嘗試使用PhantomJS,或者Chrome headless,或者docker chrome standalone –

+0

Maximilian,我已經更新了這個問題。即使對於pastebin,完整的源代碼也過於廣泛。 塔倫,我嘗試了phantomjs,但無法通過登錄表單。 –

回答

0

我終於通過使用javascript代替Selenium send_keys解決了這個問題。

driver.execute_script("document.getElementById('ToolbarOkCode').setAttribute('value', 'SM37')")