Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm very new to coding, a simple correction to my code would be greatly appreciated. Why is this not working?

JavaScript
driver.isElementPresent(By.id('select_level_8647'))

	if (id = 'select_level_8647') {
		console.log('Present')
	}
	else {
		console.log('Not Present')
	}
Posted
Updated 12-Aug-15 5:02am
v2
Comments
ZurdoDev 12-Aug-15 11:00am    
What are you trying to do? What do you mean by "this not working?"
Member 11767186 12-Aug-15 11:06am    
I'm trying to ask if the specific id "select_level_8647" is present on the page. If it's present, I wanna console.log "Present" if it's not present I want to console.log "Not Present".
ZurdoDev 12-Aug-15 11:15am    
So, are you sure you have a control with id="select_level_8647" on your page?
Member 11767186 12-Aug-15 11:17am    
Yes, i'm sure. I just changed my code to...

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


I utilized two equal signs "==" rather than "=" because apparently "=" assigns a value. I'm getting 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
ZurdoDev 12-Aug-15 11:21am    
Yes, good catch. I did not notice the = vs. ==.

But you never assign anything to id.

I am not familiar with driver but you can easily do this:

var ctl = document.getElementById("select_level_8647");
if (ctl == null)
{
alert ("Control was not found.");
}

JavaScript
if (driver.isElementPresent(By.id('select_level_8647'))) {
        console.log('Present')
    }
    else {
        console.log('Not Present')
    }
 
Share this answer
 
Equality operator under "if" is wrong. The operator = is assignment. For comparison, it should be either == or ===:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness[^].

If have no idea what is your driver object, but just the naming driver.isElementPresent suggests that it returns Boolean. But you are not using this return value, not in "if", nowhere.

—SA
 
Share this answer
 
driver.isElementPresent(document.getElementById(select_level_8647))
if (id == 'select_level_8647') {
console.log('Present')
}
else {
console.log('Not Present')
}


Okay, I fixed the operator assignment but it's still not working. Could you revise this to make it work? Your help is much appreciated.
 
Share this answer
 
Comments
Patrice T 12-Aug-15 13:58pm    
You should use "Improve question" to update your question rather that using 2 solutions for this
Sorry forgot my parenthesis around the id... help me fix this ...




JavaScript
driver.isElementPresent(document.getElementById('select_level_8647'))

    if (id == 'select_level_8647') {
        console.log('Present')
    }
    else{
        console.log('Not Present')
    }
 
Share this answer
 
v2
Not a specific solution, just a general advice.
You can see what your code is actually doing, and what it is doing wrong.
For this, you need to use a debugger.
The debugger allow you to run your code step by step, and it show you how the variables evolves.

Particularly, you should check what append to id variable before the if
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900