[問題] Selenium 登入論壇
使用 Selenium 和 Chrome 的 webdriver 來試著寫自動登入論壇的程式
不過有些論壇使用 javascript 彈出登入視窗的方式,不知如何定位
ex1: 伊莉討論區
登錄和註冊部分的網頁原始碼
<span class="pipe">|</span>
<a href="member.php?mod=register">註冊</a>
<span class="pipe">|</span>
<a href="javascript:;" onclick="lsSubmit()">登錄</a>
ex2: 卡提諾論壇
<a href="javascript:;" id="umenu" class="active"><span
class="userIcon"></span></a>
-----
我使用的程式碼分為三部分: 進入網頁, 點擊登錄, 輸入帳號密碼
# 點擊登錄
def loginClick(uri, acc, password):
regex = re.compile('user', re.IGNORECASE)
condition = False
for link in driver.find_elements_by_xpath("//a[@href=\"javascript:;\"]"):
if (link.text == '登錄' ):
link.click()
condition = True
break
elif (regex.search(str(link.find_element((By.XPATH, "..")))) != None):
link.click()
condition = True
break
if (condition == False):
print('特殊情況, 無法定位點擊')
print(uri)
# 輸入帳號密碼
def login_username(uri, acc, password):
account = driver.find_element_by_xpath("//input[@name='username']")
account.clear()
account.send_keys(acc)
pwd = driver.find_element_by_xpath("//input[@type='password']")
pwd.clear()
pwd.send_keys(password)
driver.find_element_by_xpath("//form[@method='post']").submit()
# 進入網頁
def execute():
URI = 'http://ck101.com/' # 或是伊莉討論區
data = driver.get(URI)
try:
element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH,"//input[@type='password']")))
except TimeoutException:
print(URI + ' Load page timeout.')
else:
try:
login_username(URI, 我的帳號, 我的密碼)
except:
loginClick(URI, 我的帳號, 我的密碼)
login_username(URI, 我的帳號, 我的密碼)
execute()
執行後會出現
InvalidSelectorException: Invalid locator values passed in
連 '特殊情況, 無法定位點擊' 都沒有印出
不知道是不是彈出視窗那邊出現新的事件沒被抓到 ?
--
│ ███ ▂▄▃ ││││
│ ˋ ◤Mooncat~◥││││ 「為什麼,
│ ‵ ◤ ◥▏*_▂▁ ▋ │││ 為什麼教授這麼靠盃
│ ′ 、▌█ ▊▉▏ │ 沒天理啊………」
◢ ◤◢ ◣▋◢ █ ▋▊ ▕▅▇ ◥◥*Mooncat~
◢ ▂▇ˋ█▆◤ ▂_ ▁▄▆▇▃ by mooncats
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.25.105
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1461590592.A.A8E.html
→
04/25 22:54, , 1F
04/25 22:54, 1F
→
04/25 22:54, , 2F
04/25 22:54, 2F
特地裝了 Selenium IDE 來測試, 結果跟我的思路一樣, 沒找到問題
※ 編輯: obelisk0114 (140.112.25.105), 04/26/2016 18:09:27
伊莉討論區的部分
elif 那邊好像有點問題, 註解掉就成功點擊了
但是會跑出另一個錯誤
Element is not currently interactable and may not be manipulated
※ 編輯: obelisk0114 (140.112.25.105), 04/26/2016 19:41:46
→
04/27 07:49, , 3F
04/27 07:49, 3F
※ 編輯: obelisk0114 (140.112.25.105), 05/02/2016 14:16:39
Python 近期熱門文章
PTT數位生活區 即時熱門文章