【发布时间】:2021-05-07 09:36:35
【问题描述】:
这是我的代码的一部分:
import time
from xpath_info import *
from credentials import *
from bs4 import BeautifulSoup
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager as gdm
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException, WebDriverException
class NewtonClassroomAutomation:
def __init__(self):
self.driver = None
def open_browser(self):
# Newton Classroom link
url ="https://griet.newtonclassroom.com/"
# Installing gecko driver for opening firefox
self.driver = webdriver.Firefox(executable_path=gdm().install())
# Opening the url
self.driver.get(url)
print("Browser is now opened successfully")
time.sleep(3)
def enter_credentials(self):
# Clicking on the 'Sign In with Google' button
self.driver.find_element_by_xpath(google_login_button).click()
time.sleep(3)
# Entering the email address
try:
self.driver.find_element_by_xpath(email_text_box).send_keys(email)
self.driver.find_element_by_xpath(next_button).click()
print("Entered the email address")
time.sleep(3)
except NoSuchElementException:
print("ERROR: The email address entered is either incorrect or does not exist.")
# Entering the password
self.driver.find_element_by_xpath(password_text_box).send_keys(password)
self.driver.find_element_by_xpath(next_button).click()
print("Entered the password")
time.sleep(3)
即使添加了except 子句,我也没有得到print("ERROR: The email address entered is either incorrect or does not exist"),但我得到了 selenium 显示的错误。我不知道为什么在这种情况下错误处理不起作用。
这是我的代码显示的错误:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: /html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div[1]/div/div/div/div/div[1]/div/div[1]/input
错误堆栈跟踪:
Traceback (most recent call last):
File "d:\Python Automation & Webscraping\Newton Classroom Automation\main.py", line 61, in <module>
my_classroom_bot.enter_credentials()
File "d:\Python Automation & Webscraping\Newton Classroom Automation\main.py", line 42, in enter_credentials
self.driver.find_element_by_xpath(password_text_box).send_keys(password)
File "C:\Users\ravur\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\ravur\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\ravur\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\ravur\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: /html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div[1]/div/div/div/div/div[1]/div/div[1]/input
【问题讨论】:
-
如果异常属于
NoSuchElementExceptionbtw 你的错误堆栈跟踪是什么? -
请包括 selenium 显示的错误。
-
Selenium 抛出了哪些错误,但您的 except 没有捕获到?
-
@ewong 我在问题中添加了错误
-
@Prophet NoSuchElementException
标签: python selenium web-scraping error-handling