1

I'm building a bot to print prizes of bonds from hour to hour. However, I am incurring in two errors:

  1. Since the task requires the browser to open from time to time, it ruins the experience while using the notebook. Is there a way to keep this task as a 'background' rule?

  2. I am using schedule library to set the update, but I am not quite sure it is right (even though I read the manual). Or the time set is not respected (I set to 10 minutes and the code is read from 5 to 5) or the function time it is not updated (it repeats minutes/hours/seconds).

The code is below:

import sys
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
import time
import datetime
import schedule

clock = datetime.datetime.now()

def preço():
os.chdir('C:/Users/Thiago/Desktop/Backup/Python')
options = webdriver.ChromeOptions()
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options)

driver.get("https://www.google.com/")
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("cvcb3")

time.sleep(1)

elem = driver.find_element_by_name("btnK")
elem.click()

time.sleep(2)

cvcb3 = driver.find_element_by_xpath(".//span[@jsname = 'vWLAgc']")

preço_cvcb3 = open('preço_cvcb3.txt', 'a')

preço_cvcb3.write('O preço da ação da CVC é ' + cvcb3.get_attribute("innerHTML") + ' - Extração feita ás ' + clock.strftime("%I:%M:%S %p") + '.\n')

preço_cvcb3.close()

print('O preço da ação da CVC é ' + cvcb3.get_attribute("innerHTML") + ' - Extração feita ás ' + clock.strftime("%I:%M:%S %p") + '.\n')

driver.close()

schedule.every(1).minutes.do(preço)

while True:
   schedule.run_pending()
   time.sleep(1)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
bellotto
  • 445
  • 3
  • 13
  • Why are you using hard coded time sleep instead of that always prefer to user web driver wait to avoid synchronzation issues – SeleniumUser Apr 22 '20 at 18:12
  • You can try running the Chrome driver in Headless mode. https://stackoverflow.com/a/53657649/7964299 – Naveen Apr 23 '20 at 05:23

0 Answers0