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 ; password : string
} }
let inject_username session_id way creds = let inject_username session_id creds =
(* Find username input *) (* Find username input *)
let xpath = let strat = CSS "input[name='text']" in
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 input_username = let input_username =
match find session_id xpath with match find session_id strat with
| [] -> raise (Any (fmt "Username input not found (page %d)" way)) | [] -> raise (Any (fmt "Username input not found"))
| _ as l -> | _ as l ->
if List.length l > 1 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 else List.nth l 0
in in
(* Insert the username *) (* Insert the username *)
@ -36,20 +27,15 @@ let inject_username session_id way creds =
let rec _inject_password session_id creds try_count = let rec _inject_password session_id creds try_count =
if try_count == 0 then raise (Any "Password input not found"); if try_count == 0 then raise (Any "Password input not found");
let input_password = let input_password =
match match find session_id (CSS "input[name='password']") with
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
| [] -> | [] ->
(* Retry to inject username with the second page *) (* 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); _inject_password session_id creds (try_count - 1);
None None
| _ as l -> | _ as l ->
if List.length l > 1 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) else Some (List.nth l 0)
in in
match input_password with match input_password with
@ -68,7 +54,7 @@ let inject_2fa session_id secret input =
let code = let code =
match secret with match secret with
| Some seed -> Twostep.TOTP.code ~secret:seed () | 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 in
(* Insert 2FA code *) (* Insert 2FA code *)
send_keys session_id input code; send_keys session_id input code;
@ -83,19 +69,14 @@ let login_twitter ctx username password secret =
Unix.sleep 5; Unix.sleep 5;
let creds = { username; password } in let creds = { username; password } in
(* Insert the username *) (* Insert the username *)
inject_username ctx.session_id 0 creds; inject_username ctx.session_id creds;
(* Find password input *) (* Find password input *)
inject_password ctx.session_id creds; inject_password ctx.session_id creds;
(* Detection of 2FA *) (* Detection of 2FA *)
match match find ctx.session_id (CSS "input[name='text']") with
find | [] -> print_endline "Doesn't use 2FA as no input found"
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"
| _ as l -> | _ as l ->
if List.length l > 1 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) 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) (Json.execute_payload src)
;; ;;
type strategy = XPath of string type strategy =
| XPath of string
| CSS of string
let get_strategy = function let get_strategy = function
| XPath xpath -> "xpath", xpath | XPath xpath -> "xpath", xpath
| CSS css -> "css selector", css
;; ;;
let rec wait_for_load session_id = let rec wait_for_load session_id =