Python + Selenium + 2Captcha + Error Normal Captcha - python-3.x

I'm trying to using 2Captcha to solve a normal captcha. I run my code, but it's seems like TwoCaptcha objetc has no attribute 'normal'.
from datetime import datetime
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
import shutil
import pyautogui
import time
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from twocaptcha import TwoCaptcha
chave = 'XXX'
servico = Service(ChromeDriverManager().install())
navegador = webdriver.Chrome(service=servico)
navegador.set_page_load_timeout(20) # mude 20 pela quantidade de segundos limite que você quer
carregado = False
while not carregado:
try :
navegador.get("http://www2.rio.rj.gov.br/smf/iptucertfiscal/")
carregado = True
except:
pass
navegador.maximize_window()
time.sleep(5)
foto = navegador.find_element(By.ID, 'img')
foto.screenshot('imagem.jpg')
solver = TwoCaptcha (chave)
resultado = solver.normal('imagem.jpg')
time.sleep(12)
print (resultado['code'])
I tried to run my code and I expect that someone can help me with my error. I've already installed 2captcha-python.

Related

Login into a Website with data from csv file with python selenium

I am trying to create a script that automatically logs in different accounts to a website, the login data should be taken from a CSV file. Unfortunately I get no result, maybe someone has a solution for me?
import pandas as pd
import os
from selenium import webdriver
from twocaptcha import TwoCaptcha
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
loginlist = pd.read_csv('/Volumes/GoogleDrive/Meine Ablage/Privat/py_scripts/login.csv', header=None, skiprows=\[0\], sep =',')
print(loginlist)
driver = webdriver.Chrome()
driver.get("https://freebitco.in/signup/?op=s")
time.sleep(6)
webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()
time.sleep(1)
login = driver.find_element(By.CLASS_NAME, "login_menu_button")
login.click()
time.sleep(3)
def fulfill_form(email, password):
input_email = driver.find_element(By.ID, 'login_form_btc_address')
input_password = driver.find_element(By.ID, 'login_form_password')
input_email.send_keys(email)
time.sleep(1)
input_password.send_keys(password)
time.sleep(5)
failed_attempts = \[\]
for customer in loginlist:
try:
fulfill_form(str(loginlist\[0\]), str(loginlist\[1\]))
except:
failed_attempts.append(loginlist\[0\])
pass
if len(failed_attempts) \> 0:
print("{} cases have failed".format(len(failed_attempts)))
print("Procedure concluded")
I tried several older solutions from other posts, unfortunately they led nowhere.

How to use different threads in Streamlit to use the OS buffer response from a function to another in same trigger

I'm trying to copy an image to the Windows clipboard and I do it successfully. I also need to open a website, say for example Google using Selenium in python in Streamlit.
I need that just by pressing a button I copied an image to the clipboard and paste it in the right place using Selenium.
This is the model using ThreadPoolExecutor it doesn't work.
import time
import streamlit as st
import win32clipboard
import re
from PIL import Image
from base64 import b64decode
from io import BytesIO
import pandas as pd
from whats import Cliente
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import psutil
from streamlit_quill import st_quill as text_editor
from st_aggrid import AgGrid, DataReturnMode, GridUpdateMode, GridOptionsBuilder
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from concurrent.futures import ThreadPoolExecutor
def send_to_clipboard(img_path):
image = Image.open(img_path)#path
output = BytesIO()
image.convert("RGB").save(output, "BMP")
data = output.getvalue()[14:]
#print(data)
output.close()
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
def enviar_msg():
opts = Options()
opts.add_argument("--user-data-dir=C:\...\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 4")
driver = webdriver.Chrome(options=opts)#options=opts ---headless
driver.get('https://google.com/')
driver.maximize_window()
wait = WebDriverWait(driver, 60)
enviar = wait.until(EC.presence_of_element_located((By.XPATH,'//*[#id="main"]')))
#TEXT_BOX
enviar.send_keys('')
time.sleep(5)
actions = ActionChains(driver)
actions.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
pool = ThreadPoolExecutor(max_workers=2)
with st.container():
if st.button('Enviar'):
pool.submit(send_to_clipboard, 'imagem-0.png')
pool.submit(enviar_msg)
this 'works' but is not what I'm looking for
import time
import streamlit as st
import win32clipboard
from PIL import Image
from io import BytesIO
import strings as literais
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
st.set_page_config(
page_title='MKT',
layout='wide',
initial_sidebar_state='expanded',
page_icon=literais.apple_dragon_icon, #"favicon.png" #expanded / collapsed
menu_items={
'Get help': 'https://github.com/jvcss',
'Report a bug': "https://github.com/jvcss",
'About': "App para automação whatsapp"
}
)
def send_to_clipboard(img_path):
image = Image.open(img_path)#path
output = BytesIO()
image.convert("RGB").save(output, "BMP")
data = output.getvalue()[14:]
#print(data)
output.close()
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
st.markdown(literais.css_botao_boneca_russa, unsafe_allow_html=True)
with st.container():
if st.button('Enviar'):#I NEED USE ONLY ONE BUTTON!!
send_to_clipboard(f'imagem-0.png')
st.info('imagem anexada')
if st.button('confirma'):
#send_to_clipboard(f'imagem-0.png') #this should be here!
opts = Options()
opts.add_argument("--user-data-dir=C:\\...")
driver = webdriver.Chrome(options=opts)#options=opts ---headless
driver.get('https://google.com/')
driver.maximize_window()
wait = WebDriverWait(driver, 60)
enviar = wait.until(EC.presence_of_element_located((By.XPATH,'//*[#id="main"]')))
#TEXT_BOX_CHAT
enviar .send_keys('')
time.sleep(5)
actions = ActionChains(driver)
actions.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
the last example you could do by yourself, it's just this one above using only one button.
I found an alternative way, the on_click property of the st.button is executed every time the python script is executed. so it is possible to have this trigger to capture some information before the button is actually pressed and the content within the context of its response will be executed as well. now is to think of a way to prevent the re-activation of this function in on_click when the button is actually pressed and not just a routine update. Maybe the best method is with a local variable bool I'll still think about it but the main question now has more than one way to be solved unfortunately not in the way I wanted.
with st.container():
if st.button('Enviar', on_click=send_to_clipboard('imagem-0.png')):
#build a logic to
st.info('imagem anexada')
if st.button('confirma',):
st.info('Executando Chrome')
ui_automation()

Scraping dynamic web page with selenium

I'm trying to get the links of the posts on this page, but they are apparently generated by clicking each of the post images. I'm using Selenium and beautifulsoup4 in Python 3.8.
Any idea how to get the links while selenium continues to the next pages?
url: https://www.goplaceit.com/cl/mapa?id_modalidad=1&tipo_pro//*[#id=%22gpi-property-list-container%22]/div[3]/div[1]/div[1]/imgpiedad=1%2C2&selectedTool=list#12/-33.45/-70.66667
after clicking on the image it opens a new tab with the following type of shortening url: https://www.goplaceit.com/propiedad/6198212
which sends me to a url type:
https://www.goplaceit.com/cl/propiedad/venta/departamento/santiago/6198212-depto-con-1d-1b-y-terraza-a-pasos-del-metro-toesca-bodega
My code:
from bs4 import BeautifulSoup
from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import winsound
from timeit import default_timer as timer
from selenium.webdriver.common.keys import Keys
start = timer()
PROXY = "PROXY" # IP:PORT or HOST:PORT
path_to_extension = r"extension"
options = Options()
#options.add_argument("--incognito")
options.add_argument('load-extension=' + path_to_extension)
#options.add_argument('--disable-java')
options.headless = False
prefs = {"profile.default_content_setting_values.notifications" : 2}
prefs2 = {"profile.managed_default_content_settings.images": 2}
prefs.update(prefs2)
prefs3 = {"profile.default_content_settings.cookies": 2}
prefs.update(prefs3)
options.add_experimental_option("prefs",prefs)
options.add_argument("--start-maximized")
options.add_argument('--proxy-server=%s' % PROXY)
driver = webdriver.Chrome('chromedriver.exe', options=options)
driver.get('https://www.goplaceit.com/cl/')
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="root"]/nav/div/div[2]/div[1]/button'))).click()
correo = driver.find_element(By.XPATH, '//*[#id="email"]')
correo.send_keys("Mail")
contraseña = driver.find_element(By.XPATH, '//*[#id="password"]')
contraseña.send_keys("password")
contraseña.send_keys(Keys.ENTER)
time.sleep(7)
elem.driver.find_element(By.XPATH, '//*[#id="gpi-main-landing-search-input"]/div/input')
elem.click()
elem.send_keys("keywords")
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="gpi-main-landing-search-input"]/div/div[1]/ul/li[1]/a/div/div[1]'))).click()
buscador.send_keys(Keys.ENTER)
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="root"]/div/div/div[1]/div[2]/div/div[1]/div/div[1]/button'))).click()
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="custom-checkbox"]'))).click()
page_number = 0
max_page_number = 30
while page_number<=max_page_number:
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(),"paginator-btn-right")]'))).click()
You can get easily the urls by clicking on an image, saving your url, coming back to the first page and repeating this for all the images:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver.get("https://www.goplaceit.com/cl/mapa?id_modalidad=1&tipo_propiedad=1%2C2&selectedTool=list#8/-33.958/-71.206")
images = WebDriverWait(driver, 30).until(EC.presence_of_all_elements_located((By.XPATH, "//div[#class='sc-iyvyFf ljSqTz']//img")))
urls = []
for i, image in enumerate(images):
window_before = driver.window_handles[0]
image.click()
driver.implicitly_wait(2)
window_after = driver.window_handles[i+1]
driver.switch_to.window(window_after)
urls.append(driver.current_url)
driver.switch_to.window(window_before)

How to log in with selenium webdriver

Hello I am trying to log in automatically with webdriver to this webpage :
https://www.oddsportal.com/login/
Here is my script :
#!/usr/bin/python3
# -*- coding: utf­-8 ­-*-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from concurrent.futures import ThreadPoolExecutor
import pandas as pd
options = Options()
#options.headless = True
options.add_argument("window-size=1400,800")
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("start-maximized")
options.add_argument("enable-automation")
options.add_argument("--disable-infobars")
options.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(options=options)
driver.get("https://www.oddsportal.com/login/")
driver.find_element_by_id("login-username1").send_keys("ahmedaao")
driver.find_element_by_id("login-password1").send_keys("password")
driver.find_element_by_name("login-submit").click()
I have a problem with with the last line of code. The webpage open up then username and password are charged but then I am not logged in.
I find the solution :
driver.find_element_by_xpath('//button[#type="submit"]').click()

dBase for my website keeps giving me error "Invalid syntax"

I have a web page I have been developing and I am now stuck getting told syntax is wrong, I cannot find the error can someone please help me find my error? Here is my code for the database that is giving me my error:
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup
from models import *
import os # File I/O
import time
import shutil
import glob
import configparser
config_parser = configparser.ConfigParser()
config_parser.read("config.ini")
pdownload_dir = os.path.abspath('./prism_downloads/')
dt = str(datetime.datetime.now())
filelist = glob.glob(download_dir + '/*.html')
dbpath = ('./db')
def db_Prism():
database.connect()
database.create_tables([Prism], safe=False)
database.close()
for root, dir, files in os.walk(pdownload_dir):
for file in files:
print(file)
file_markup = ''
with open(os.path.abspath(os.path.join(pdownload_dir, file)), 'r') as html:
file_markup = html.read()
if file_markup == '':
print('ERROR: File was not read')
print('Reading {0} into BS4'.format(file))
soup = BeautifulSoup(file_markup, 'html.parser')
print('File parsed')
data = []
table = soup.find('table')
rows = table.find_all('tr') # 18th row is header row
cols = rows[0].find_all('td')
cols = [ele.text.strip() for ele in cols]
database.connect()
for row in rows[0:]:
d = row.find_all('td')
d = [ele.text.strip() for ele in d]
data.append([ele for ele in d if ele]) # Get rid of empty values
Prism.create(pmt_id=(d[1]),
old_status=d[3],
new_status=(d[4]),
last_updated=float(d[5])
Line 96 database.close()
Now here is the error message from my console:
C:\Users\Documents\NetBeansProjects\BudgetHome>python prism.py
File "prism.py", line 96
database.close()
^
SyntaxError: invalid syntax
C:\Users\Documents\NetBeansProjects\BudgetHome>

Resources