Reputation: 17
I am attempting to use selenium to gather all game titles within a selected users profile so that I can later select one at random using random.choice to suggest them playing it utilizing a discord bot. However the help I'm seeking currently would just be to print the game titles to console, I can handle the discord.py integration.
So far I am able to return the entire list of games, however it is also including unwanted information such as play time and links.
I would like to separate out just the game title and store each in a list, again to be referenced later to choose one at random.
So far I have attempted using a .split('\n') however the number of separate lines varies based upon the information returned and not all users will have the same number of titles on their page.
I have also attempted using the xpath of:
//*[@id="game_107410"]/div[2]/div[1]/div[1]/div
However as easily visible this is referencing a specific game id and won't return other titles on the same page.
My current code is shown below. Thank you in advanced for any suggestions or direction on learning to solve this.
from selenium import webdriver
PATH = 'C:/Program Files (x86)/chromedriver.exe'
game_titles ={}
driver = webdriver.Chrome(PATH)
driver.get('https://steamcommunity.com/id/Desmoo88/games/?tab=all')
game_list= driver.find_element_by_xpath('//*[@id="games_list_rows"]').text
print(game_list)
driver.quit()
Upvotes: 1
Views: 52
Reputation: 7563
You can use this selector for grab text the game title:
div.gameListRowItemName.ellipsis
Try following code:
driver.get('https://steamcommunity.com/id/Desmoo88/games/?tab=all')
game_titles =[]
wait = WebDriverWait(driver, 20)
title_list = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'div.gameListRowItemName.ellipsis')))
for title in title_list:
game_titles.append(title.text)
print(len(game_titles))
print(game_titles)
Following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Upvotes: 1
Reputation: 3433
You can try like this:
from selenium import webdriver
driver = webdriver.Chrome(executable_path="path to chromdriver.exe")
driver.maximize_window()
driver.implicitly_wait(10)
driver.get("https://steamcommunity.com/id/Desmoo88/games/?tab=all")
rows = driver.find_elements_by_class_name("gameListRow") # Collect all rows
for row in rows:
name = row.find_element_by_xpath(".//div[contains(@class,'Name')]").text # Collect the Name in that particular row. To find an element within an element use a `.` before `xpath`.
print(name)
Arma 3
Garry's Mod
Counter-Strike: Global Offensive
...
Zeno Clash 2
ZombieRun
Upvotes: 0