Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.5 kB
2
Indexable
Never
require 'selenium-webdriver'
require 'socksify/http'
require 'watir'
require 'colorize'
require 'byebug'

def accept_appoitment(driver)
  begin
    process = driver.find_element(id: 'idSede')
    options = process.find_elements(tag_name: 'option')
    options.first.click
    
    accept_button = driver.find_element(id: 'btnSiguiente')
    driver.execute_script("enviar()", accept_button)
  
    phone_field = driver.find_element(id: 'txtTelefonoCitado')
    phone_field.send_keys('+40766513834')
    email_field = driver.find_element(id: 'emailUNO')
    email_field.send_keys('dezsirazvan@gmail.com')
    email_2_field = driver.find_element(id: 'emailDOS')
    email_2_field.send_keys('dezsirazvan@gmail.com')
  
    accept_button = driver.find_element(id: 'btnSiguiente')
    driver.execute_script("enviar()", accept_button)
  rescue => exception
    puts('error'.blue)
  else
    return    
  end
end

counter = 1
main_counter = 1
while true
  puts("Try number: #{counter}".yellow)
  # options = Selenium::WebDriver::Chrome::Options.new
  # options.add_argument('--headless')
  args = ['-headless']

  options = Selenium::WebDriver::Chrome::Options.new(args: args)
  driver = Selenium::WebDriver.for :chrome, options: options
  # driver = Selenium::WebDriver.for :chrome, switches: %w[--incognito]
  # driver.manage.window.move_to(1000,1200)
  # driver.manage.window.minimize
  driver.navigate.to "https://icp.administracionelectronica.gob.es/icpplus/index.html"
  # this part is to select automatic Barcelona from the list
  begin
    element = driver.find_element(name: 'form')
    options = element.find_elements(tag_name: 'option')
    options.each { |option| option.click if option.text == 'Barcelona' }
  # click btnAceptar to go to the next step
    accept_button = driver.find_element(id: 'btnAceptar')
    sleep 0.5
    driver.execute_script("envia()", accept_button)

    
    # select the first process
    process = driver.find_element(id: 'tramiteGrupo[0]')
    options = process.find_elements(tag_name: 'option')
    # 4058 nie green/ 4096 tie
    options.each do |option| 
      if option.attribute('value') == '4058'
        sleep 0.5
        option.click
        driver.execute_script("cargaMensajesTramite()", option)
      end
    end

    # select the real process
    process = driver.find_element(id: 'tramiteGrupo[1]')
    options = process.find_elements(tag_name: 'option')
    options.each do |option| 
      if option.attribute('value') == '4096'
        sleep 0.5
        option.click
        driver.execute_script("cargaMensajesTramite()", option)
      end
    end

    # click btnAceptar to go to the next step
    accept_button = driver.find_element(id: 'btnAceptar')
    sleep 0.5
    driver.execute_script("envia()", accept_button)

    # accept_button.click

    # click btnEntrar to go to the next step
    accept_button = driver.find_element(id: 'btnEntrar')
    sleep 0.5
    driver.execute_script("document.forms[0].submit()", accept_button)

    # this part is to select the passport radio button
    passport_button = driver.find_element(id: 'rdbTipoDocPas')
    sleep 0.5
    passport_button.click

    # add nie and name
    nie_field = driver.find_element(id: 'txtIdCitado')
    sleep 0.5
    nie_field.send_keys('Nie number')
    name_field = driver.find_element(id: 'txtDesCitado')
    sleep 0.5
    name_field.send_keys('Full name')

    # click btnEnviar to go to the next step
    accept_button = driver.find_element(id: 'btnEnviar')
    sleep 0.5
    driver.execute_script("envia()", accept_button)

    # click btnEnviar to go to the next step
    accept_button = driver.find_element(id: 'btnEnviar')
    sleep 0.5
    driver.execute_script("enviar('solicitud')", accept_button)
  rescue
    puts('Error from them...'.red)
    driver.close()

    320.downto(1) do |n|
      puts n.to_s.green
      sleep 1 # second
    end

    next
  end

  # accept_button.click

  # find error message
  begin
    message = driver.find_element(class: 'mf-msg__info')
  rescue => exception
    accept_appoitment(driver)

    puts('I found a cita!!!'.green)
    1.times do
      `say Cita Found !!!!!!!!!`
      puts(Time.now.to_s.green)
    end
  else
    unless message.text.split('.').first.include?("En este momento no hay citas disponibles")
      accept_appoitment(driver)

      puts('I found a cita!!!'.green)
      5.times do
        `say Cita Found !!!!!!!!!`
      end
    else
      puts("#{Time.now.to_s} Cita not found :(".red)
      driver.close()
    end
  end

  counter += 1
end