Click here to Skip to main content
14,735,876 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?

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 6: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.");
}
Member 11767186 12-Aug-15 11:24am
   
Could you help me assign something to ID with this below? I'm at work researching everywhere. I appreciate your help!


driver.isElementPresent(document.getElementById('select_level_8647'))
if (id == 'select_level_8647') {
console.log('Present')
}
else {
console.log('Not Present')
}
ZurdoDev 12-Aug-15 11:35am
   
Don't use isElementPresent. Use that little bit of code I gave you.
Patrice T 12-Aug-15 12:11pm
   
You should use the "improve question" (in green) when you have sample code changes.

if (driver.isElementPresent(By.id('select_level_8647'))) {
        console.log('Present')
    }
    else {
        console.log('Not Present')
    }
   
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
   
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.
   
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 ...




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

    if (id == 'select_level_8647') {
        console.log('Present')
    }
    else{
        console.log('Not Present')
    }
   
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
   
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