1

My for loop only seems to only find the first item from https://public.tableau.com/en-gb/gallery/?tab=viz-of-the-day&type=viz-of-the-day instead of looping through all dates and titles. If I print(viz) I can see different elements, but that doesn't seem to be carried through.

driver.get("https://public.tableau.com/en-gb/gallery/?tab=viz-of-the-day&type=viz-of-the-day")
wait = WebDriverWait(driver, 10)

time.sleep(10)

vizzes = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".gallery-items-list div.gallery-list-item-container")))
for viz in vizzes:
    print(viz)

    #publish date
    date_id = driver.find_element_by_css_selector('[data-test-id="published-date"]').text
    print(date_id)

    #name of the viz
    viz_name = driver.find_element_by_xpath("//a[contains(@href, '/en-gb/gallery/')]").text
    print(viz_name)

For xpath I tried using

viz_name = driver.find_element_by_xpath(".//a[contains(@href, '/en-gb/gallery/')]").text

and

viz_name = driver.find_element_by_xpath("//*[contains(@href, '/en-gb/gallery/')]").text

which yielded the same result.

1
  • The line date_id = driver.find_element_by_css_selector('[data-test-id="published-date"]').text doesn't reference viz anywhere, so of course it returns the same thing every time Commented Jan 25, 2020 at 18:20

2 Answers 2

3

You need to correct with viz.find_element.. instead of driver.find_element..:

#publish date
date_id = viz.find_element_by_css_selector('[data-test-id="published-date"]').text
print(date_id)

#name of the viz
viz_name = viz.find_element_by_xpath("//a[contains(@href, '/en-gb/gallery/')]").text
print(viz_name)
Sign up to request clarification or add additional context in comments.

Comments

1

Although it appears that you have already defined explicit wait, you used hardcoded delay which you can get rid of to make the script robust. Here is another way of doing the same using css selector:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

link = "https://public.tableau.com/en-gb/gallery/?tab=viz-of-the-day&type=viz-of-the-day"

with webdriver.Chrome() as driver:
    wait = WebDriverWait(driver, 10)
    driver.get(link)
    for item in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "[class$='item-container']"))):
        viz_name = item.find_element_by_css_selector("[class$='item-title-left'] > a").text
        date_id = item.find_element_by_css_selector("[data-test-id='published-date']").text
        print(viz_name,date_id)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.