1

I'm attempting to use Selenium to print out a list of users I'm subscribed to on a website, of which there are 3. The following code only prints out the first 2 of the three

from xml.dom.minidom import Element
from selenium import webdriver
from selenium.webdriver.support import wait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService;
from webdriver_manager.chrome import ChromeDriverManager;
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.mouse_button import MouseButton
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()));
#Chrome client should now start up


driver.get("website-url"); #Go to desired website
title = driver.title;
if title == "WebsiteTitle":
    driver.implicitly_wait(5)

email_box = driver.find_element(By.NAME, "email"); #finds username block
pass_box = driver.find_element(By.NAME, "password"); #finds password block

email_box.send_keys('username'); #enter username
pass_box.send_keys('password'); #enter password

submit_button = driver.find_element(By.CSS_SELECTOR, "button[type='submit']"); #finds submit button
submit_button.click(); #clicks it

wait = WebDriverWait(driver, 15);
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='content']/div[1]/div/div/div/div[1]/h1/span"))); #waits 15sec until the "Home" button on the home page can be clicked

def print_subs_list():          #function to print list of users you are subscribed to
    driver.get("website-subs-url");
    subscriptions_list = driver.find_elements(By.XPATH, "//*[@id='content']/div[1]/div/div[4]/div/div/div[1]/div/div[2]/div/div[2]/div/div[1]/a/div")
    for value in subscriptions_list:
        print(value.text)

print_subs_list()

driver.quit()

Now, changing the XPath in subscriptions_list to //*[@id='content']/div[1]/div/div[4]/div/div/div[2]/div/div[2]/div/div[2]/div/div[1]/a/div will print out the 3rd result only. However, my desired result would be to print all of the subscribed users, as there will definitely be more than 3. How do I change it so that it will print out all of the subscribed users, regardless of the amount?

javery1337
  • 23
  • 5

1 Answers1

1

You identify all the three desired elements using the xpath:

//*[@id='content']/div[1]/div/div[4]/div/div//div/div/div[2]/div/div[2]/div/div[1]/a/div

Your effective line of code will be:

subscriptions_list = driver.find_elements(By.XPATH, "//*[@id='content']/div[1]/div/div[4]/div/div//div/div/div[2]/div/div[2]/div/div[1]/a/div")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352