我想運行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="/^[^<">]*$/"
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)
是不是一個'iframe'裏面?類名真的是'vm.submitLogin()'? –
是的。這是否意味着無法從iframe中訪問html元素? – tin
@PedroLobito是的HTML是完全如何在網頁上 – tin