2013-05-05 109 views
1

名字我嘗試遠程登錄到Twitter和使用硒和鉻webdriver的鳴叫發現Twitter的元素,但我似乎無法得到它之前,我做任何事情我想只測試是否我可以寫入用戶名和密碼區域,但我甚至不能這樣做。不能使用硒和鉻的webdriver

這裏是我的代碼:

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 


driver = webdriver.Chrome() 

driver.get("https://twitter.com") 
elem = driver.find_element_by_name("session[username_or_email]") 
elem.send_keys("test") 
elem.send_keys(Keys.RETURN) 

當我嘗試這一點,打開命令提示符下運行chromedriver.exe然後打開鉻窗口,但再沒有什麼事情發生,這讓我這個錯誤消息:

Traceback (most recent call last): 
File "C:\Python27\Project\Auth2.py", line 10, in <module> 
    elem.send_keys("test") 
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line  162, in send_keys 
    self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': typing}) 
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 228, in _execute 
    return self._parent.execute(command, params) 
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 165, in execute 
    self.error_handler.check_response(response) 
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 152, in check_response 
    raise exception_class(message, screen, stacktrace) 
NoSuchWindowException: Message: u'no such window: target window already closed\nfrom  unknown error: web view not found\n (Session info: chrome=26.0.1410.64)\n (Driver info:  chromedriver=0.8,platform=Windows NT 5.1 SP3 x86)' 

誰能告訴我什麼是問題,以及如何解決它!

+0

我得到它的工作我想我只是需要使用'driver.find_element_by_id',而不是'driver.find_element_by_name'和它的工作就好了! – Serial 2013-05-05 22:35:14

+0

但現在我想知道如果我可以做到這一點出窗口彈出? – Serial 2013-05-05 22:38:20

回答

0

是的,find_element_by_id,find_element_by_xpathfind_element_by_css_selector應該用在這種特殊情況下。

我懷疑elem = driver.find_element_by_name("session[username_or_email]")將不會工作,因爲方括號。

對於誰比誰需要處理使用Selenium方括號,使用CSS選擇器或XPath如下面的例子應該更容易(方括號中的類名在這個例子中):

<label class="username js-username"> 
    <span>Username or email</span> 
    <input class="session[username_or_email]" type="text" autocomplete="on" /> 
</label> 
# I suspect this won't work? 
driver.find_element_by_class_name("session[username_or_email]") 

# won't work, as the special meaning of square brackets in css selector 
driver.find_element_by_css_selector(".session[username_or_email]") 

# use these 
driver.find_element_by_css_selector("[class='session[username_or_email]']") 
driver.find_element_by_xpath("//*[@class='session[username_or_email]']") # same as usual 
0

這是爲我工作

EmailElement = driver.find_element_by_name('session[username_or_email]') 

PasswordElement = driver.find_element_by_name('session[password]') 

EmailElement.send_keys('email/username_goes_here') 

PasswordElement.send_keys('password_goes_here')