首页 > 解决方案 > what happens when find_elements can't find the class?

问题描述

I am trying to find a particular class on a website. The class is sometimes present and sometimes it is absent. So when the class is present, it takes a few seconds for the script to locate the element(logo). When the class is not present,the script runs for a long time and then end.

Why is that? is there any way to speed it up when the class doesn't exist?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep

chrome_path = r"C:\Users\peter\Desktop\chromedriver.exe"
driver = webdriver.Chrome(executable_path=r"C:\Users\peter\Desktop\chromedriver.exe")
driver.get("https://example.com/app/login")

driver.minimize_window()
driver.implicitly_wait(300)

input_email = driver.find_element_by_xpath("//input[@type='email']")
input_email.send_keys('example@gmail.com')

input_password = driver.find_element_by_xpath("//input[@type='password']")
input_password.send_keys('example')

click_login = driver.find_element_by_xpath("//button[@type='submit']")
click_login.click()

driver.find_element_by_id("schedule-today").click()
sleep(2)

logo = driver.find_elements_by_xpath( "//*[contains(@class, 'lbl_lesson_status label label-info lbl_lesson_open')]" );

if not logo:
 print("empty")

f = open("reserved_date", "a+")
for i in logo:
  opendate = i.get_attribute("data-t-start-local");
  f.write((opendate)+'\n')
  print(opendate)
driver.close()

标签: pythonseleniumxpath

解决方案


You Need To Add Wait And Add Try Except for example if element not found throw message and quit that script

I Simply Code For You!

Try This Code:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import *
from selenium.webdriver.common.keys import Keys
import time

chrome_path = r"C:\Users\peter\Desktop\chromedriver.exe"
driver = webdriver.Chrome(executable_path=r"C:\Users\peter\Desktop\chromedriver.exe")
driver.get("https://example.com/app/login")
driver.minimize_window()


try: 
    input_email = WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//input[@type='email']")))
    input_email.send_keys('example@gmail.com')
except (TimeoutException,NoSuchElementException):
    print('There is No Email Input!')
    quit()

try:
    input_password = WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//input[@type='password']")))
    input_password.send_keys('example')
except (TimeoutException,NoSuchElementException):
    print('There is No Password Input!')
    quit()

try:
    click_login = WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[@type='submit']")))
    click_login.click()
except (TimeoutException,NoSuchElementException):
    print('There is No Login Button!')
    quit()



try:
    WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#schedule-today")))
    time.sleep(2)
except (TimeoutException,NoSuchElementException):
    print("Can't Find schedule-today id!")
    quit()


try:
    logo = WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//*[contains(@class, 'lbl_lesson_status label label-info lbl_lesson_open')]")))
    f = open("reserved_date", "a+")
    for i in logo:
      opendate = i.get_attribute("data-t-start-local");
      f.write((opendate)+'\n')
      print(opendate)
except (TimeoutException,NoSuchElementException):
    print("Can't Find Logo Button!")
    quit()

driver.close()

推荐阅读