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('selec t_level_86 47'))
if (id = 'select_level_8647') {
console.log('Present')
}
else {
console.log('Not Present')
}
driver.isElementPresent(By
if (id = 'select_level_8647') {
console.log('Present')
}
else {
console.log('Not Present')
}
ASKER
Just utilized this as you said...
driver.isElementPresent(By .id('selec t_level_86 47'))
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/sele nium/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
driver.isElementPresent(By
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/sele
at Module._compile (module.js:460:26)
at Object.Module._extensions.
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
ASKER
Sorry, didn't fully listen to what you said.
driver.isElementPresent(do cument.get ElementByI d('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.
driver.isElementPresent(do
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')
}
ASKER
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-serve r-standalo ne-2.47.1. jar
// node Checkout.js
var webdriver = require('selenium-webdrive r'),
By = require('selenium-webdrive r').By,
until = require('selenium-webdrive r').until;
var driver = new webdriver.Builder()
.forBrowser('firefox')
.build();
driver.get('http://www.lkdjfldkjf;d.com');
driver.manage().window().m aximize();
driver.isElementPresent(do cument.get ElementByI d('select_ level_8647 '))
if (id == 'select_level_8647') {
console.log('Present')
}
else {
console.log('Not Present')
}
//the program shown below automates the purchase of one free ticket
//Instructions: Start Selenium Web Server from command line
// java -jar ~/Downloads/selenium-serve
// node Checkout.js
var webdriver = require('selenium-webdrive
By = require('selenium-webdrive
until = require('selenium-webdrive
var driver = new webdriver.Builder()
.forBrowser('firefox')
.build();
driver.get('http://www.lkdjfldkjf;d.com');
driver.manage().window().m
driver.isElementPresent(do
if (id == 'select_level_8647') {
console.log('Present')
}
else {
console.log('Not Present')
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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-serve r-standalo ne-2.47.1. jar
// node Checkout.js
var webdriver = require('selenium-webdrive r'),
By = require('selenium-webdrive r').By,
until = require('selenium-webdrive r').until;
var driver = new webdriver.Builder()
.forBrowser('firefox')
.build();
driver.get('http://www.jdl;jf;dkjf.com');
driver.manage().window().m aximize();
if( driver.isElementPresent(By .id('selec t_level_86 47')))
{
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('jkdlf kj')))
{
console.log('Present')
}
else {
console.log('Not Present')
}
and get a returned "Not Present" printed to terminal?
//the program shown below automates the purchase of one free ticket
//Instructions: Start Selenium Web Server from command line
// java -jar ~/Downloads/selenium-serve
// node Checkout.js
var webdriver = require('selenium-webdrive
By = require('selenium-webdrive
until = require('selenium-webdrive
var driver = new webdriver.Builder()
.forBrowser('firefox')
.build();
driver.get('http://www.jdl;jf;dkjf.com');
driver.manage().window().m
if( driver.isElementPresent(By
{
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
{
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.
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>
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')