2017-06-24 245 views
1

我試圖製作一個Python程序,它將自動登錄到我學校的網站。Python Selenium無法找到元素

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="loginidtext"]"}

相關的代碼段引發錯誤是::

BCnumber = driver.find_element_by_xpath('//*[@id="loginidtext"]') 
BCnumber.send_keys('loginid') 

的網址是: https://matrix.tjc.edu.sg/?topleft=toprow.php&bottomright=bottomrow.php

我已經嘗試使用:不過,我有一個錯誤返回

driver.switch_to 

切換到相關的div,但同樣的錯誤是返回...

+1

that loginidtext field in inside the second frame。因此,我們需要切換到索引爲1的幀,然後發送鍵 –

回答

1

這裏是回答你的問題:

由於定位//*[@id="loginidtext"]topwindow iframe中,我們必須先切換到的iframe如下:

from selenium import webdriver 
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary 

binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe') 
driver = webdriver.Firefox(firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe") 
driver.get('https://matrix.tjc.edu.sg/?topleft=toprow.php&bottomright=bottomrow.php') 
driver.maximize_window() 
driver.implicitly_wait(20) 
driver.switch_to.frame("topwindow") 
BCnumber = driver.find_element_by_xpath('//*[@id="loginidtext"]') 
BCnumber.send_keys('loginid') 

讓我知道這個答案是否是你的問題。

+0

您是否執行了代碼並檢查它是否正常工作?因爲它不適合我。 – RAJ

+0

@RAJ你是否發現了一個錯誤?你在哪裏看到錯誤? 謝謝 – DebanjanB

+0

元素不存在錯誤被拋出!你執行了程序並檢查了嗎?它傳遞給你嗎? – RAJ

相關問題