我的for循环似乎只能从https://public.tableau.com/en-gb/gallery/?tab=viz-of-the-day&type=viz-of-the-day找到第一项,而不是循环浏览所有日期和标题。如果我print(viz)
我可以看到不同的元素,但这似乎没有实现。
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)
对于我尝试使用的xpath
viz_name = driver.find_element_by_xpath(".//a[contains(@href, '/en-gb/gallery/')]").text
和
viz_name = driver.find_element_by_xpath("//*[contains(@href, '/en-gb/gallery/')]").text
产生了相同的结果。
最佳答案
您需要使用viz.find_element..
而不是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)
关于python - Selenium Python for loop仅获得第一项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59911921/