Rasukarusan / shellnium

:dizzy: Selenium Webdriver for Bash (or Zsh).
https://shellnium-site.vercel.app
MIT License
173 stars 21 forks source link

There is a way to change of iframe? #8

Closed rodrigo-sys closed 2 years ago

rodrigo-sys commented 2 years ago

I meant, there is an equivalente to this function of selenium

driver.switch_to.frame(iframe)

there is some elements of the doms that can not be accede unless you switch frame

Rasukarusan commented 2 years ago

@rodrigo-sys Oh, I missed it :innocent: I added switch_to_frame and switch_to_parent_frame. c8c46e4

Thanks!


------ example ----------

demo.sh

#!/usr/bin/env bash
source ./lib/selenium.sh

main() {
    # Open the page.
    navigate_to 'file:///Users/YOUR_PATH/test.html'

    switch_to_frame 'exampleFrame'
    local button=$(find_element 'id' 'countButton')
    click $button
}

main

test.html

<!DOCTYPE html>
<html lang="en">
<body>
<iframe id="exampleFrame"
    width="300"
    height="200"
    src="./frame.html">
</body>
</html>

frame.html

<!DOCTYPE html>
<html lang="en">
<body>
  <script>
    function countUp() {
      var elm = document.getElementById('count')
      var count = parseInt(elm.innerText)
      elm.innerText = count + 1
    }
  </script>
  <button id="countButton" onclick="countUp()">click me</button>
  <span id="count">1</span>
</body>
</html>
screenshot_ 2021-11-27 15 16 47
jontxux commented 2 years ago

And in case the iframe has no id, is there any way to search by selector or tag name? demo.sh

source ./lib/selenium.sh

main () {
    navigate_to 'file:///tmp/tmp.Wr9JK2E4Tq/test.html'
    iframe=$(find_element 'css selector' 'body > iframe')
    switch_to_frame $iframe
    local button=$(find_element 'id' 'countButton')

    click $button

    delete_session
}

main
++ find_element 'css selector' 'body > iframe'
++ local 'property=css selector'
++ local 'value=body > iframe'
++ curl -s -X POST -H '"Content-Type:' 'application/json"' -d '{"using":"css selector", "value": "body > iframe"}' http://localhost:9515/session/3e533e205d2461ac8b9301683c520dbd/element
++ jq -r .value.ELEMENT
+ iframe=0.7264159897532416-1
+ switch_to_frame 0.7264159897532416-1
+ local id=0.7264159897532416-1
+ curl -s -X POST -H '"Content-Type:' 'application/json"' -d '{"id":"0.7264159897532416-1"}' http://localhost:9515/session/3e533e205d2461ac8b9301683c520dbd/frame
{"sessionId":"3e533e205d2461ac8b9301683c520dbd","status":8,"value":{"message":"no such frame\n  (Session info: chrome=96.0.4664.45)\n  (Driver info: chromedriver=96.0.4664.45 (76e4c1bb2ab4671b8beba3444e61c0f17584b2fc-refs/branch-heads/4664@{#947}),platform=Linux 5.15.0-1-amd64 x86_64)"}}

Trying the following way gives the message that it does not find the frame

Rasukarusan commented 2 years ago

@jontxux Hi, jontxux. thanks your comment! Now, I supported switch to frame by number. 414da6b Let's try this!

image

demo.sh

#!/usr/bin/env bash
source ./lib/selenium.sh

main() {
    # Open the page.
    navigate_to 'file:///Users/tanakanaoto/Documents/github/shellnium/test.html'

    # switch to frame by number
    switch_to_frame 0
    local button=$(find_element 'id' 'countButton')
    click $button

    # back to main window
    switch_to_parent_frame

    # switch to frame by number
    switch_to_frame 1
    local button=$(find_element 'id' 'countButton')
    click $button
}

main

test.html

<!DOCTYPE html>
<html lang="en">
<body>
  <iframe src="./frame.html"></iframe>
  <iframe src="./frame.html"></iframe>
</body>
</html>

frame.html

<!DOCTYPE html>
<html lang="en">
<body>
  <script>
    function countUp() {
      var elm = document.getElementById('count')
      var count = parseInt(elm.innerText)
      elm.innerText = count + 1
    }
  </script>
  <button id="countButton" onclick="countUp()">click me</button>
  <span id="count">1</span>
</body>
</html>
Rasukarusan commented 2 years ago

@jontxux Or you can try this.

demo.sh

main() {
    # Open the page.
    navigate_to 'file:///Users/tanakanaoto/Documents/github/shellnium/test.html'

    frame=$(find_element 'css selector' 'body > iframe')
    switch_to_frame $frame
    local button=$(find_element 'id' 'countButton')
    click $button
}
main

test.html

<!DOCTYPE html>
<html lang="en">
<body>
  <iframe id="exframe" src="./frame.html"></iframe>
</body>
</html>

6413679