2017-04-21 200 views
0

我想運行python selenium並自動登錄過程。自動登錄

from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from datetime import datetime, date 

currentDate = ("{:%Y%m%d}".format(datetime.now())) 
usernameStr = 'email' 
passwordStr = 'passoword' 

browser = webdriver.Chrome() 
browser.get('http://website.com/en/entry?' + 'date=' + currentDate) 
delay = 3 


loginButton = browser.find_element_by_xpath('//*[@id="global-header"]/div[2]/ul/li[2]/a') 
loginButton.click() 
delay = 3 
Username = browser.find_element_by_name('Username or Email Address') 
Username.send_keys(usernameStr) 
password = browser.find_element_by_name('password') 
password.send_keys(passwordStr) 
nextButton = browser.find_element_by_class_name('vm.submitLogin()') 
nextButton.click() 

這裏是我試圖訪問

<span class="input-wrapper"> 
<input type="email" placeholder="Username or Email Address" 
autocapitalize="none" autocomplete="on" autocorrect="off" spellcheck="false" 
ng-model="vm.username" ng-pattern="/^[^<&quot;>]*$/" 
ng-required="true" did-disable-validate=" 
"ng-focus="vm.resetUsername()" 
class="ng-pristine ng-invalid ng-invalid-required ng-valid-pattern 
ng-touched" tabindex="0" 
required="required" aria-required="true" aria-invalid="true"></span> 

我試過的名字,CSS選擇器,XPath來訪問HTML代碼的HTML,但似乎沒有任何工作。這是一個登錄橫幅,而不是一個標準的HTML頁面,所以idk,如果這有所作爲。

雅呵這裏有錯誤,我得到

Traceback (most recent call last): 
    File "/Users/aj/Desktop/streakscraper/login.py", line 20, in <module> 
    Username = browser.find_element_by_name('Username or Email Address') 
    File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 378, in find_element_by_name 
    return self.find_element(by=By.NAME, value=name) 
    File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 784, in find_element 
    'value': value})['value'] 
    File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 249, in execute 
    self.error_handler.check_response(response) 
    File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 193, in check_response 
    raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"Username or Email Address"} 
    (Session info: chrome=57.0.2987.133) 
    (Driver info: chromedriver=2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b),platform=Mac OS X 10.12.4 x86_64) 
+0

是不是一個'iframe'裏面?類名真的是'vm.submitLogin()'? –

+0

是的。這是否意味着無法從iframe中訪問html元素? – tin

+0

@PedroLobito是的HTML是完全如何在網頁上 – tin

回答

1

你應該切換到正確的iframe,然後嘗試匹配html

要切換到不同的iframe使用:

driver.switch_to.frame(driver.find_element_by_name("iframe_name")) 

或簡單地:

driver.switch_to.frame("iframe_name") 

您還可以通過索引來訪問它:

driver.switch_to.frame(0) # 1, 2, etc.. if you have multiple iframes 

要切換回主內容的使用:

driver.switch_to.default_content() 
+1

非常感謝您的幫助 – tin

+0

不客氣@ tin10,但我認爲您的問題比這更深刻,注意您的代碼,當您嘗試通過'name'選擇元素時,如果它們沒有該屬性'name',你可能想要使用'css','xpath','type','id'或者任何其他屬性的不同類型的選擇。看看https://selenium-python.readthedocs.io/locating-elements.html –

+0

雅這是我一直在使用的資源我讓它工作。非常感謝iframe的信息,我一直在試圖弄清楚這一點。 – tin