0

I'm working on a project in IntelliJ that uses PhantomJS and Selenide to automate web browsing activity. To run Javascript commands, we use the executeJavascript() method in our Java code. We call this method many times in our code to execute the Javascript commands that we want.

For this particular case, we are making a program that should automatically log us into Footlocker.

Code for program:

public Boolean doInBackground() {
    WebDriver driver = getWebDriver();

    System.out.println("Running Footlocker");
    open(account.getEarlyLink()); //opens URL
    System.out.println("Running Footlocker");
    if (!loggedIn) {
        WebDriverRunner.setWebDriver(driver);

        $("#guest_welcome_login").shouldBe(visible).click();

        $("#login_container").shouldBe(visible);
    }
             executeJavaScript("$('html').find('iframe').eq(1).contents().find('#login_email').val('%s')", account.getUsername());
        executeJavaScript(String.format("$('html').find('iframe').eq(1).contents().find('#login_password').val('%s')", account.getPassword()));
        executeJavaScript("$('html').find('iframe').eq(1).contents().find('button.button.cta_button').click()");
        $("#member_welcome").shouldBe(visible);
}

When we run the program, none of the executeJavascript() commands are actually executed, but when we run each command one by one in the IntelliJ expression executer (in the debugger), they work.

Anyone know what's happening here?

Bill Wang
  • 1
  • 2

2 Answers2

0

Did you try to SWITCH into frame?

Typically in Selenium you need to switch into frame/iframe to get access to its elements. Like this:

switchTo(frame(1));
executeJavaScript("$('#login_email').val('%s')", account.getUsername());
Andrei Solntsev
  • 480
  • 2
  • 8
0
//Adding child under parent div using executeJavaScript
SelenideElement parentDiv = $x(
                "<parent element>");

        Selenide.executeJavaScript("var newInput = document.createElement('input');"
                + "newInput.setAttribute('type', 'file');" + "arguments[0].appendChild(newInput);", parentDiv);
Anil Jain
  • 57
  • 3