Link to home
Start Free TrialLog in
Avatar of AdamHannisick
AdamHannisick

asked on

Javascript If Else Statement

I'm very new to Javascript.  Why is the code below not working?  A correction to my code would be greatly appreciated.

driver.isElementPresent(By.id('select_level_8647'))
      if (id = 'select_level_8647') {
            console.log('Present')
      }
      else {
            console.log('Not Present')
      }
Avatar of hielo
hielo
Flag of Wallis and Futuna image

>> Why is the code below not working?
Are you getting a specific error code/description?

>> By.id('...')
I don't know if you are using some library that has a definition for By(), but if that is not the case, you most likely meant:
driver.isElementPresent( document.getElementById('select_level_8647') )

>>  if (id = 'select_level_8647')
A single "=" is used for assignment, not to test for equality. You need at least two "=" signs to check for equality.  Also, be sure to initialize id somewhere first.
 if (id == 'select_level_8647')
Avatar of AdamHannisick
AdamHannisick

ASKER

Just utilized this as you said...

driver.isElementPresent(By.id('select_level_8647'))
      if (id == 'select_level_8647') {
            console.log('Present')
      }
      else {
            console.log('Not Present')
      }

Got the following error message...

ReferenceError: id is not defined
    at Object.<anonymous> (/Users/adamhannisick/selenium/wait.js:27:6)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3
Sorry, didn't fully listen to what you said.  

driver.isElementPresent(document.getElementById('select_level_8647'))
      if (id == 'select_level_8647') {
            console.log('Present')
      }
      else {
            console.log('Not Present')
      }

I utilized the document.getelementbyid as you said, I'm getting a document is not defined error.
Like I said, you need to initialize id somewhere first before you test for equality. I don't know where driver.isElementPresent() is defined, but if it returns a boolean, then you probably need:
if( driver.isElementPresent( document.getElementById('select_level_8647') ){
            console.log('Present')
      }
else {
            console.log('Not Present')
}

Open in new window

Okay, here's what i'm working with.  I really appreciate your help by the way.

//the program shown below automates the purchase of one free ticket

//Instructions:  Start Selenium Web Server from command line
   // java -jar ~/Downloads/selenium-server-standalone-2.47.1.jar
   // node Checkout.js

var webdriver = require('selenium-webdriver'),
    By = require('selenium-webdriver').By,
    until = require('selenium-webdriver').until;

var driver = new webdriver.Builder()
    .forBrowser('firefox')
    .build();

driver.get('http://www.lkdjfldkjf;d.com');

driver.manage().window().maximize();


driver.isElementPresent(document.getElementById('select_level_8647'))
      if (id == 'select_level_8647') {
            console.log('Present')
      }
      else {
            console.log('Not Present')
      }
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay thank you!  So after trying this...

//the program shown below automates the purchase of one free ticket

//Instructions:  Start Selenium Web Server from command line
   // java -jar ~/Downloads/selenium-server-standalone-2.47.1.jar
   // node Checkout.js

var webdriver = require('selenium-webdriver'),
    By = require('selenium-webdriver').By,
    until = require('selenium-webdriver').until;

var driver = new webdriver.Builder()
    .forBrowser('firefox')
    .build();

driver.get('http://www.jdl;jf;dkjf.com');

driver.manage().window().maximize();


if( driver.isElementPresent(By.id('select_level_8647')))
 {
            console.log('Present')
}
else {
            console.log('Not Present')
}


It runs successfully however it prints to terminal "Present" before the webpage finishes even loading up.  Also, if I want to get a return value of "Not Present" what would I have to change?  Would the specific element on the web page have to not be present?  Or could I just mess up the id like this...

if( driver.isElementPresent(By.id('jkdlfkj')))
 {
            console.log('Present')
}
else {
            console.log('Not Present')
}


and get a returned "Not Present" printed to terminal?
>>It runs successfully however it prints to terminal "Present" before the webpage finishes even loading up
I can't help you with that problem.  The original problem was a javascript problem, but it has now escalated to "how do I do X in npmjs (that is what you seem to be using)", which I am not familiar with.

Having said that, I did look around.  My best guess is that you will need js-dom with a done:function(){...} callback option(look at the third example).  Within the done you can then test for the presence of whatever it is you are interested in.
try putting your if statement in the windows onload
<script type="text/javascript">
window.addEventListener('load', function() {
  initialise()
});

function initialise()
{
  if( driver.isElementPresent(By.id('jkdlfkj')))  {
    console.log('Present')
  }
  else {
    console.log('Not Present')
  }
}
</script>

Open in new window