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')
      }
AdamHannisickAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
>> 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')
0
AdamHannisickAuthor Commented:
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
0
AdamHannisickAuthor Commented:
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

hieloCommented:
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

0
AdamHannisickAuthor Commented:
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')
      }
0
hieloCommented:
OK, then based on your original post try:
if( driver.isElementPresent(By.id('select_level_8647') ) )
 {
            console.log('Present')
}
else {
            console.log('Not Present')
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AdamHannisickAuthor Commented:
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?
0
hieloCommented:
>>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.
0
Julian HansenCommented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.