Youjia
Newbie
- Dec 18, 2025
- 1
- 0
Writing a Selenium script to auto-fill contact forms (example: https://cornerstonesalina.com/elements/contact-form/), hitting a math captcha snag like "5 + 3" or "10 ÷ 2". Using regex to extract nums/op and lambdas to compute.
Original solver:
Problem: Works on +-×*, but division "10 ÷ 2" matches regex (group(2)='÷'), yet computes 12! Form rejects. Full script OK otherwise (fake data, JS clicks).
My attempted fix (works locally):
Full context script snippet (adapt IDs):
What am I still missing? Unicode ops regex quirks? Better parser (no lambdas)? Edge cases like 0÷0? Share your form-filler tweaks!
Original solver:
Python:
def solve_math_captcha(captcha_text):
match = re.search(r'(\d+)\s*([+\-×÷*/])\s*(\d+)', captcha_text)
if not match:
return None
num1, op, num2 = int(match.group(1)), match.group(2), int(match.group(3))
ops = {'+': lambda a,b: a+b, '-': lambda a,b: a-b, '×': lambda a,b: a*b, '*': lambda a,b: a*b}
return ops.get(op, lambda a,b: a+b)(num1, num2) # <-- BUG: ÷ defaults to +
Problem: Works on +-×*, but division "10 ÷ 2" matches regex (group(2)='÷'), yet computes 12! Form rejects. Full script OK otherwise (fake data, JS clicks).
My attempted fix (works locally):
- Added '÷': //, '/': // to ops
- Wait for captcha text load: EC.text_to_be_present_in_element
Python:
def solve_math_captcha(captcha_text):
print(f"Parsing: {captcha_text}")
match = re.search(r'(\d+)\s*([+\-×÷*/])\s*(\d+)', captcha_text)
if not match:
return None
num1, op, num2 = int(match.group(1)), match.group(2), int(match.group(3))
ops = {
'+': lambda a,b: a + b,
'-': lambda a,b: a - b,
'×': lambda a,b: a * b,
'*': lambda a,b: a * b,
'÷': lambda a,b: a // b if b else 0,
'/': lambda a,b: a // b if b else 0
}
return ops.get(op, lambda a,b: a + b)(num1, num2)
# In main:
captcha_container = wait.until(EC.presence_of_element_located((By.ID, "element_avia_7_3")))
wait.until(lambda d: re.search(r'\d+\s*[+\-×÷*/]\s*\d+', captcha_container.text)) # Load wait
answer = solve_math_captcha(captcha_container.text)
Full context script snippet (adapt IDs):
Code:
# ... (options, get url, fill fields, checkbox JS click)
if answer:
driver.find_element(By.ID, "avia_7_3").send_keys(str(answer))
submit_btn = driver.find_element(By.CSS_SELECTOR, "input[type='submit']")
driver.execute_script("arguments[0].click();", submit_btn)
# Wait success .avia-form-success
What am I still missing? Unicode ops regex quirks? Better parser (no lambdas)? Edge cases like 0÷0? Share your form-filler tweaks!