2017-06-24 252 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

相关问题