use css selector instead of xpath

This commit is contained in:
Mylloon 2023-05-14 22:52:36 +02:00
parent 61346496b9
commit ccf780e11e
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 17 additions and 33 deletions

View file

@ -6,24 +6,15 @@ type credentials =
; password : string
}
let inject_username session_id way creds =
let inject_username session_id creds =
(* Find username input *)
let xpath =
match way with
| 0 ->
XPath
"/html/body/div[1]/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div/div/div/div[5]/label/div/div[2]/div/input"
| 1 ->
XPath
"/html/body/div[1]/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[2]/label/div/div[2]/div/input"
| _ -> raise (Any "Unknown page to use for injecting username")
in
let strat = CSS "input[name='text']" in
let input_username =
match find session_id xpath with
| [] -> raise (Any (fmt "Username input not found (page %d)" way))
match find session_id strat with
| [] -> raise (Any (fmt "Username input not found"))
| _ as l ->
if List.length l > 1
then raise (Any "Too many element found as the username input")
then raise (Any "Too many elements found as the username input")
else List.nth l 0
in
(* Insert the username *)
@ -36,20 +27,15 @@ let inject_username session_id way creds =
let rec _inject_password session_id creds try_count =
if try_count == 0 then raise (Any "Password input not found");
let input_password =
match
find
session_id
(XPath
"/html/body/div[1]/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div/div[3]/div/label/div/div[2]/div[1]/input")
with
match find session_id (CSS "input[name='password']") with
| [] ->
(* Retry to inject username with the second page *)
inject_username session_id 1 creds;
inject_username session_id creds;
_inject_password session_id creds (try_count - 1);
None
| _ as l ->
if List.length l > 1
then raise (Any "Too many element found as the password input")
then raise (Any "Too many elements found as the password input")
else Some (List.nth l 0)
in
match input_password with
@ -68,7 +54,7 @@ let inject_2fa session_id secret input =
let code =
match secret with
| Some seed -> Twostep.TOTP.code ~secret:seed ()
| None -> raise (Any "No TOTP code given, but TOTP required")
| None -> raise (Any "No TOTP code given, but 2FA code required")
in
(* Insert 2FA code *)
send_keys session_id input code;
@ -83,19 +69,14 @@ let login_twitter ctx username password secret =
Unix.sleep 5;
let creds = { username; password } in
(* Insert the username *)
inject_username ctx.session_id 0 creds;
inject_username ctx.session_id creds;
(* Find password input *)
inject_password ctx.session_id creds;
(* Detection of 2FA *)
match
find
ctx.session_id
(XPath
"/html/body/div[1]/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[2]/label/div/div[2]/div/input")
with
| [] -> print_endline "Don't use 2FA input as input not found"
match find ctx.session_id (CSS "input[name='text']") with
| [] -> print_endline "Doesn't use 2FA as no input found"
| _ as l ->
if List.length l > 1
then raise (Any "Too many element found as 2FA input")
then raise (Any "Too many elements found as 2FA input")
else inject_2fa ctx.session_id secret (List.nth l 0)
;;

View file

@ -53,10 +53,13 @@ let execute_sync session_id src =
(Json.execute_payload src)
;;
type strategy = XPath of string
type strategy =
| XPath of string
| CSS of string
let get_strategy = function
| XPath xpath -> "xpath", xpath
| CSS css -> "css selector", css
;;
let rec wait_for_load session_id =