2016-03-24 77 views
1

嗨我想用selenium登錄到namecheap.com,但無法選擇登錄表單。我選擇元素時出現這個錯誤:無法用python selenium登錄到namecheap

selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with 

但我可以看到我選擇了好的xpath。 這裏是我的代碼:

from selenium import webdriver 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.common.exceptions import TimeoutException 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.common import action_chains 
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 
import time 

cookie_file_path = 'cookie.txt' 

dcap = dict(DesiredCapabilities.PHANTOMJS) 
dcap["phantomjs.page.settings.userAgent"] =("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36") 

# driver = webdriver.PhantomJS(desired_capabilities=dcap,service_args=['--ssl-protocol=any','--ignore-ssl-errors=true','--cookies-file={}'.format(cookie_file_path)]) 
driver=webdriver.Firefox() 
driver.set_window_size(1024, 768) 
username='test' 
password='test123' 

driver.get('https://www.namecheap.com/myaccount/login.aspx') 

driver.find_element_by_xpath("//input[@name='LoginUserName']").send_keys(username) 
driver.find_element_by_xpath('//input[@name="LoginPassword"]').send_keys(password) 
loginButtonXpath = "//input[@type='submit']" 
driver.find_element_by_xpath(loginButtonXpath).click() 
driver.save_screenshot('testing.png') 

這裏是HTML:

    <div class="sign-in-form"> 
        <fieldset> 
         <input type="text" name="LoginUserName" title="Username" placeholder="Username" autocapitalize="off" autocorrect="off" class="input removespecialchars handlereturn" maxlength="20" /> 
         <input type="password" name="LoginPassword" title="Password" placeholder="Password" class="input handlereturn" maxlength="100" /> 
         <input type="hidden" name="hidden_LoginPassword" /> 
         <a href="https://www.namecheap.com/myaccount/login.aspx" id="ctl00_ctl00_ctl00_ctl00_base_content_web_base_content_topNavLoginLink" class="head-loginb ncRedirectButton btn sign-in-btn btn-block" rel="nofollow">Sign In</a> 
         <a href="https://ap.www.namecheap.com/ResetPassword" id="ctl00_ctl00_ctl00_ctl00_base_content_web_base_content_passwordRemiderLink" title="Password Reminder">Forgot your password?</a> 
        </fieldset> 
        </div> 
       </div> 
      </li> 
      <li class="hidewhenloggedin"><a href="/myaccount/signup.aspx">Sign Up</a></li> 
      <li class="expandable signed-in hidewhennotloggedin user-menu"> 

你能幫助我嗎? 謝謝

回答

0

看來你的xPath定位器(用於登錄/用戶名)是失敗的原因。

嘗試使用下面的CSS定位器..

Username 
css=div.group > input[name="LoginUserName"] 

Password 
css=div.group > input[name="LoginPassword"] 

一般來說,相對於CSS定位器XPath定位被認爲是更脆弱。所以,使用CSS定位器會很好。

+0

No ...我得到了同樣的錯誤 - 與driver.find_element_by_css_selector('input [name =「LoginUserName」]')。send_keys('aaaaa') –

+0

Ahh ...我的壞...現在工作.. 。非常感謝你! –

+0

鍵是使用整個CSS選擇器。很高興它適合你:) – TestingWithArif

0

你進口WebDriverWaitexpected_conditions但你不使用它們

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[@name='LoginUserName']")).send_keys(username) 

這將等待長達10秒的元素是可見的。

+0

我糾正了我的代碼 - TestingWithArif答案是好的。謝謝 –