0

Hey guys I have a problem with the if-else conditions.I was creating the following bot that searches and alerts me when theres a appointment available but I can't make the if-else conditons work at the final lines of the code,the bot doesn't respect the if-else conditions I've tried changing several times the code but no idea of how to resolve this problem.Thanks for the help.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
import time
import re
import os
import winsound

duration = 2000  # milliseconds
freq = 900  # Hz

lets_go = True

while lets_go == True:
    browser = webdriver.Chrome()
    browser.implicitly_wait(30) 
    browser.maximize_window()  
    chrome_options = Options()
    chrome_options.add_experimental_option("detach", True)
    browser.get("https://icp.administracionelectronica.gob.es/icpplus/index.html")
    browser.verificationErrors = [] 
    
    
    cookie_kill = browser.find_element_by_id("cookie_action_close_header")
    cookie_kill.click()
    #sleep(1)
    madrid = browser.find_element_by_xpath('//*[@id="form"]/option[34]')
    madrid.click()
    #sleep(1)
    accept = browser.find_element_by_id("btnAceptar")
    accept.click()
    #sleep(1)
    browser.execute_script("window.scrollTo(0,document.body.scrollHeight)")
    tramites_group = browser.find_element_by_xpath('/html/body/div[1]/div[2]/main/div/div/section/div[2]/form[1]/div[3]/div[1]/div[2]/div/fieldset/div[2]/select/option[3]')
    tramites_group.click()
    sleep(1)
    
    aceptar = browser.find_element_by_id("btnAceptar")
    aceptar.click()
    sleep(1)
   
    browser.execute_script("window.scrollTo(0,document.body.scrollHeight)")
    enter_button = browser.find_element_by_id('btnEntrar')
    enter_button.click()
    sleep(1)
    
    passport = browser.find_element_by_id("rdbTipoDocPas")
    passport.click()
    passport_number = browser.find_element_by_id("txtIdCitado").send_keys("123456789")
    person_name = browser.find_element_by_id("txtDesCitado").send_keys("BORIS JOHNSON")
    person_birth = browser.find_element_by_id("txtAnnoCitado").send_keys("1900")
    nationality = browser.find_element_by_xpath('/html/body/div[1]/div[2]/main/div/div/section/div[2]/form/div/div/div[1]/div[5]/div/div/div/div/span/select/option[200]')
    nationality.click() 
    browser.execute_script("window.scrollTo(0,document.body.scrollHeight)")    
    sleep(1)
    
    enviar = browser.find_element_by_id("btnEnviar")
    browser.execute_script("arguments[0].click();", enviar)
    sleep(1)
    
    enviar = browser.find_element_by_id("btnEnviar")
    browser.execute_script("arguments[0].click();", enviar)
    sleep(1)
    
    no_appointments = browser.page_source.find("En este momento no hay citas disponibles.")
    
    if no_appointments:
       browser.close()
       time.sleep(120)
       
    else:
         winsound.Beep(freq, duration)
         print("found")
         lets_go = False
         break

1 Answer 1

1

page_source returns a normal Python string. The find method of a string does not return a boolean True/False. It returns the starting character number if found, and -1 if not found. Thus, you want:

    no_appointments = browser.page_source.find("En este momento no hay citas disponibles.")
    
    if no_appointments >= 0:
       browser.close()
       time.sleep(120)

You might consider whether it makes more sense to write:

    if "En este momento no hay citas disponibles" in browser.page_source:
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.