Skip to content

Commit

Permalink
Merge pull request #34 from DanielFillol/daniel
Browse files Browse the repository at this point in the history
Create MakeElementVisible
  • Loading branch information
DanielFillol authored Jul 19, 2024
2 parents 8c89edc + 539664c commit da6d435
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
14 changes: 14 additions & 0 deletions goSpider.go
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,20 @@ func (nav *Navigator) SaveImageBase64(selector, outputPath, prefixClean string)
return base64Data, nil
}

// MakeElementVisible changes the style display of an element to nil
func (nav *Navigator) MakeElementVisible(selector string) error {
nav.Logger.Printf("Making CAPTCHA response field with selector: %s visible\n", selector)
err := chromedp.Run(nav.Ctx,
chromedp.Evaluate(fmt.Sprintf(`document.querySelector('%s').style.display = ""`, selector), nil),
)
if err != nil {
nav.Logger.Printf("Error - Failed to make element visible: %v\n", err)
return fmt.Errorf("error - failed to make element visible: %v", err)
}
nav.Logger.Printf("Element with selector: %s is now visible\n", selector)
return nil
}

// Close closes the Navigator instance and releases resources.
// Example:
//
Expand Down
38 changes: 38 additions & 0 deletions goSpider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,44 @@ func TestSaveImageBase64(t *testing.T) {
})
}

func TestMakeElementVisible(t *testing.T) {
server := startTestServer()
defer server.Close()

nav := setupNavigator(t)
defer nav.Close()

err := nav.OpenURL(server.URL + "/test.html")
if err != nil {
t.Errorf("OpenURL error: %v", err)
return
}

id, err := nav.GetElementAttribute("#divInfraCaptcha > div > iframe", "data-hcaptcha-widget-id")
if err != nil {
t.Errorf("GetElementAttribute error: %v", err)
return
}

err = nav.MakeElementVisible("#h-captcha-response-" + id)
if err != nil {
t.Errorf("MakeElementVisible error: %v", err)
return
}

err = nav.WaitForElement("#h-captcha-response-"+id, nav.Timeout)
if err != nil {
t.Errorf("MakeElementVisible error: %v", err)
return
}

err = nav.FillField("#h-captcha-response-"+id, "54203432300")
if err != nil {
t.Errorf("FillField error: %v", err)
return
}
}

// Won't pass on test because 2FA requires input on the terminal by the user, for that reason alone the test will fail
//// TestLoginGoogle tests google single logon
//func TestLoginGoogle(t *testing.T) {
Expand Down
8 changes: 6 additions & 2 deletions server/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ <h1>Main Content</h1>
<div id="loginStatus"></div>

<!-- Captcha -->
<div id="divInfraCaptcha">
<div data-sitekey="test-site-key"></div>
<div id="divInfraCaptcha" class="infraAreaDados" data-url-verificar-captcha="controlador_ajax.php?acao_ajax=verifica_estado_captcha">
<div class="h-captcha" data-sitekey="ea7d5a3e-4785-4957-9b59-fcaddfc0b074">
<iframe src="https://newassets.hcaptcha.com/captcha/v1/12d4359/static/hcaptcha.html#frame=checkbox&amp;id=0sxde7a0i8pm&amp;host=eproc2g.tjrs.jus.br&amp;sentry=true&amp;reportapi=https%3A%2F%2Faccounts.hcaptcha.com&amp;recaptchacompat=true&amp;custom=false&amp;hl=pt-BR&amp;tplinks=on&amp;pstissuer=https%3A%2F%2Fpst-issuer.hcaptcha.com&amp;sitekey=ea7d5a3e-4785-4957-9b59-fcaddfc0b074&amp;theme=light&amp;origin=https%3A%2F%2Feproc2g.tjrs.jus.br" tabindex="0" frameborder="0" scrolling="no" title="Widget contendo caixa de seleção para desafio de segurança hCaptcha" data-hcaptcha-widget-id="0sxde7a0i8pm" data-hcaptcha-response="" style="pointer-events: auto; width: 303px; height: 78px; overflow: hidden;"></iframe>
<textarea id="g-recaptcha-response-0sxde7a0i8pm" name="g-recaptcha-response" style="display: none;"></textarea>
<textarea id="h-captcha-response-0sxde7a0i8pm" name="h-captcha-response" style="display: none;"></textarea></div>
<input type="hidden" id="hdnInfraCaptcha" name="hdnInfraCaptcha" value="0">
</div>

<!-- Dynamic Content Page -->
Expand Down

0 comments on commit da6d435

Please sign in to comment.