使用 Python 时出现 Selenium Traceback 错误

Selenium Traceback Error while using Python

我正在使用脚本自动执行每天早上上班必须进行的调查。当我 运行 时,它 运行 很好,并且没有问题地完成。在命令提示符中 运行ning 之后,我不断收到此回溯错误。

代码:

from selenium import webdriver
import time
import warnings


warnings.filterwarnings('ignore')
driver = webdriver.Chrome(executable_path="./drivers/chromedriver.exe")
driver.get('https://quickentry.teamhgs.com/')

EmployeeID = *EEID*
LastName = *LASTNAME*

# Wait for webpage to load and click usa employee button
time.sleep(1)
USAEmployeeButton = driver.find_element_by_xpath('/html/body/section/div/div/div/div/div/div[2]/a[1]')
USAEmployeeButton.click()

# input employee button and last name then click verify
EID = driver.find_element_by_xpath('//*[@id="empId"]')
EID.send_keys(EmployeeID)
last = driver.find_element_by_xpath('//*[@id="empLastName"]')
last.send_keys(LastName)
VerifyEmployeeID = driver.find_element_by_xpath('//*[@id="getemp"]')
VerifyEmployeeID.click()

# wait for the verification to finish and click proceed
time.sleep(10)
ProceedButton = driver.find_element_by_xpath('//*[@id="proceed"]')
ProceedButton.click()

# click no on all of them and then next and submit and Yes
Q1No = driver.find_element_by_xpath('/html/body/section/div/div/div/div/div/div[2]/div[2]/div[2]/div[2]/label/p')
Q1No.click()
Next = driver.find_element_by_xpath('//*[@id="next-question"]')
Next.click()
Q2No = driver.find_element_by_xpath('/html/body/section/div/div/div/div/div/div[2]/div[2]/div[2]/div[2]/label/p')
Q2No.click()
Next.click()
Q3No = driver.find_element_by_xpath('/html/body/section/div/div/div/div/div/div[2]/div[2]/div[2]/div[2]/label/p')
Q3No.click()
Next.click()
Q4No = driver.find_element_by_xpath('/html/body/section/div/div/div/div/div/div[2]/div[2]/div[2]/div[2]/label/p')
Q4No.click()
Submit_Button = driver.find_element_by_xpath('//*[@id="submit"]')
Submit_Button.click()
time.sleep(1)
Yes_Button = driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/button[2]')
Yes_Button.click()

# check if it worked
get_confirmation_div_text = driver.find_element_by_css_selector('.admit-msg text-center')
Success_Message = "Based on your answers, you are granted access to the site for today’s shift.  Please complete this questionnaire before arriving on-site for any shift moving forward until further notice. Stay safe."
if get_confirmation_div_text.text == Success_Message:
    print("Test Was Successful")
else:
    print("Test wasn't successful")

# Close page when finished
time.sleep(2)
driver.close()

错误:

C:\Users\USERNAME\PycharmProjects\MorningAutomation\venv\Scripts\python.exe C:/Users/USERNAME/PycharmProjects/MorningAutomation/QuickEntry.py

DevTools listening on ws://127.0.0.1:51345/devtools/browser/85f8fdee-9bc5-42ca-adb3-29fe65733ca5
[26408:26860:1213/081313.939:ERROR:chrome_browser_main_extra_parts_metrics.cc(226)] crbug.com/1216328: Checking Bluetooth availability started. Please report if there is no report that this ends.
[26408:26860:1213/081313.939:ERROR:chrome_browser_main_extra_parts_metrics.cc(229)] crbug.com/1216328: Checking Bluetooth availability ended.
[26408:26860:1213/081313.940:ERROR:chrome_browser_main_extra_parts_metrics.cc(232)] crbug.com/1216328: Checking default browser status started. Please report if there is no report that this ends.
[26408:26388:1213/081313.940:ERROR:device_event_log_impl.cc(214)] [08:13:13.940] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[26408:26388:1213/081313.942:ERROR:device_event_log_impl.cc(214)] [08:13:13.941] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[26408:26388:1213/081313.946:ERROR:device_event_log_impl.cc(214)] [08:13:13.945] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[26408:26860:1213/081313.959:ERROR:chrome_browser_main_extra_parts_metrics.cc(236)] crbug.com/1216328: Checking default browser status ended.
Traceback (most recent call last):
  File "C:\Users\alex.olivas\PycharmProjects\MorningAutomation\QuickEntry.py", line 51, in <module>
    get_confirmation_div_text = driver.find_element_by_css_selector('.admit-msg text-center')
  File "C:\Users\alex.olivas\PycharmProjects\MorningAutomation\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 808, in find_element_by_css_selector
    return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
  File "C:\Users\alex.olivas\PycharmProjects\MorningAutomation\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1244, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\alex.olivas\PycharmProjects\MorningAutomation\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "C:\Users\alex.olivas\PycharmProjects\MorningAutomation\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".admit-msg text-center"}
  (Session info: chrome=96.0.4664.93)
Stacktrace:
Backtrace:
        Ordinal0 [0x003C6903+2517251]
        Ordinal0 [0x0035F8E1+2095329]
        Ordinal0 [0x00262848+1058888]
        Ordinal0 [0x0028D448+1233992]
        Ordinal0 [0x0028D63B+1234491]
        Ordinal0 [0x002B7812+1406994]
        Ordinal0 [0x002A650A+1336586]
        Ordinal0 [0x002B5BBF+1399743]
        Ordinal0 [0x002A639B+1336219]
        Ordinal0 [0x002827A7+1189799]
        Ordinal0 [0x00283609+1193481]
        GetHandleVerifier [0x00555904+1577972]
        GetHandleVerifier [0x00600B97+2279047]
        GetHandleVerifier [0x00456D09+534521]
        GetHandleVerifier [0x00455DB9+530601]
        Ordinal0 [0x00364FF9+2117625]
        Ordinal0 [0x003698A8+2136232]
        Ordinal0 [0x003699E2+2136546]
        Ordinal0 [0x00373541+2176321]
        BaseThreadInitThunk [0x762DFA29+25]
        RtlGetAppContainerNamedObjectPath [0x772E7A9E+286]
        RtlGetAppContainerNamedObjectPath [0x772E7A6E+238]

我一直在寻找类似的东西,但 none 这些决议似乎对我有用。

.admit-msg text-center 不是有效的 。 您需要用点替换 space 字符,即 .

实际上,您的 coe 行将是:

get_confirmation_div_text = driver.find_element_by_css_selector('.admit-msg.text-center')