Selenium find_element_by_id not functioning as expected

Viewed 59
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"
browser = webdriver.Chrome(PATH)

browser.get("https://orteil.dashnet.org/cookieclicker/")

time.sleep(3)


cookie = browser.find_element_by_id("bigCookie")
cookie_count = browser.find_element_by_id("cookies")

actions = ActionChains(browser)
actions.click(cookie)

productNames = []
for i in range(18):
    productNames.append(browser.find_element_by_id("productName" + str(i)))

print("product names: ", productNames)

productPrices = []
for i in range(18):
    productPrices.append(browser.find_element_by_id("productPrice" + str(i)))

print("product names: ", productNames)

def purchase(count,cps):
    pass

def start_clicking():
    for i in range(500):
        actions.perform()
        count = int(cookie_count.text.split(" ")[0])
        cps = cookie_count.text.split("per second : ")[-1]

start_clicking()
browser.quit()

I'm trying to create a bot to automatically play cookie clicker. For now I'm just trying to get the bot to click the cookie and then create a list of the product names and prices. When I run I get numerous lines of incomprehensible text. For example when i is equal to 4 I want it to append Factory to the list since when I go into inspect I find this line in the elements tab:

<div class="title" id="productName4">Factory</div> 

nd I also want to add the price into another list e.g. <span class="price" id="productPrice4">130,000</span> so 130000 in this case.

1 Answers

By doing

for i in range(18):
    productNames.append(browser.find_element_by_id("productName" + str(i)))

and

productPrices = []
for i in range(18):
    productPrices.append(browser.find_element_by_id("productPrice" + str(i)))

You are getting web elements, not their texts.
If you wish to collect those texts you should do

for i in range(18):
    productNames.append(browser.find_element_by_id("productName" + str(i)).text)

and

productPrices = []
for i in range(18):
    productPrices.append(browser.find_element_by_id("productPrice" + str(i)).text)
Related