George Xu
George Xu

Reputation: 13

can't find my element (selenium beginner)

new programmer here- I am trying to make a cookieclicker bot using selenium. Cookie clicker is a website game linked below

http://orteil.dashnet.org/cookieclicker/

I am attempting to locate the element, like so

 WebElement element = driver.findElement(By.id("upgrades"));

and then I call it here:

upgrades = element.findElements(By.className("enabled"));

I want to make a list of "upgrades" that are "enabled". However, I get a null pointer exception on the second piece of code- but on the website there clearly is an element called "upgrades".

Thank you for your time!

edit:

this is the block of code from the game that I am trying to click

<div id="upgrades" class="storeSection upgradeBox"><div onclick="Game.UpgradesById[0].click(event);" class="crate upgrade enabled" onmouseout="Game.setOnCrate(0);Game.tooltip.shouldHide=1;" onmouseover="if (!Game.mouseDown) 

and I am trying to fish out these "enabled" upgrades

<div onclick="Game.UpgradesById[0].click(event);" class="crate upgrade enabled" onmouseout="Game.setOnCrate(0);Game.tooltip.shouldHide=1;" onmouseover="if (!Game.mouseDown) {Game.setOnCrate(this);Game.tooltip.dynamic=1;Game.tooltip.draw(this,function(){return Game.crate(Game.UpgradesById[0],'store',undefined,undefined,1)();},'store');Game.tooltip.wobble();}" id="upgrade0" style="background-position:0px 0px;"></div>

Upvotes: 0

Views: 89

Answers (1)

Shrini
Shrini

Reputation: 193

I just checked on the website, your code requires modification.

Use the below code:

WebElement element = driver.findElement(By.id("upgrades"));
Actions acc=new Actions(driver);
acc.moveToElement(element).build().perform();
List<WebElement> upgrades = element.findElements(By.className("enabled"));

Let me know if this works for you.

Upvotes: 1

Related Questions