0

I have the following problem. I wrote a program that solves captcha. If I run it locally, it works without problems, but when I upload it to the vps server, the following error pops up:

Traceback (most recent call last):
  File "/home/heniu/stooq_captcha_solver/captcha_solver.py", line 61, in <module>
    code = solver.get_result(id)
  File "/home/heniu/.local/lib/python3.10/site-packages/twocaptcha/solver.py", line 469, in get_result
    raise NetworkException
twocaptcha.solver.NetworkException

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

from twocaptcha import TwoCaptcha

chrome_options = Options()

chrome_options.add_argument("--headless=chrome")

driver = webdriver.Chrome(
    executable_path='chromedriver',
    options=chrome_options
)

url = "https://stooq.pl/db/h/"

print(f'Łączę ze stroną {url}')
driver.get(url)
driver.maximize_window()

time.sleep(3)
driver.implicitly_wait(5)

cookie_button = driver.find_element(
    By.XPATH,
    "/html/body/div/div[2]/div[1]/div[2]/div[2]/button[1]/p"
)
cookie_button.click()

time.sleep(3)
driver.implicitly_wait(15)

print('Pobieram obrazek')
download_link = driver.find_element(
    By.XPATH,
    "/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr/td/table/tbody/tr/td/table[5]/tbody/tr/td/table/tbody/tr[2]/td[1]/table/tbody/tr[5]/td[3]/a"
)
download_link.click()

time.sleep(3)
driver.implicitly_wait(5)

captcha_img = driver.find_element(By.ID, "cpt_cd")

with open("captcha.png", "wb") as captcha:
    captcha.write(captcha_img.screenshot_as_png)

print('Obrazek pobrany')
print('Rozwiązuję captchę')

solver = TwoCaptcha('api_key')

captcha_solve = solver.normal("captcha.png")

id = solver.send(file="captcha.png")
time.sleep(30)

code = solver.get_result(id)
print("Captcha rozwiązana")

captcha_text = driver.find_element(
    By.XPATH,
    "/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr/td/table/tbody/tr/td/table[5]/tbody/tr/td/div[1]/div[1]/table/tbody/tr/td/table/tbody/tr[5]/td/input"
)
captcha_text.send_keys(code)

captcha_button = driver.find_element(
    By.XPATH,
    "/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr/td/table/tbody/tr/td/table[5]/tbody/tr/td/div[1]/div[1]/table/tbody/tr/td/table/tbody/tr[6]/td/input"
)
captcha_button.click()

time.sleep(3)
driver.implicitly_wait(5)

print('Pobieram plik')
download_button = driver.find_element(By.ID, "cpt_gt")
download_button.click()

time.sleep(90)
driver.close()

In line:

solver = TwoCaptcha('api_key')

I have right api_key

I have try to change waiting time, but that not work.

On VPS I install google_chrome_stable.

Captcha images download is right, whenever I run programm, new image is downloaded

Heniu
  • 13
  • 3

1 Answers1

0

Sorry for trouble. I solved it. It was enough to extend the waiting time for a solution quite significantly. Earlier I add 10-15 seconds to wait time. 60 seconds working

Heniu
  • 13
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 11 '23 at 12:30