Reputation: 11
I write script in python for getting multiple urls. Multiple urls are print in console,but i want when multiple urls are print in console then show total number of urls.
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome(executable_path="C:\Program Files\Python39\libs\chromedriver.exe")
driver.maximize_window()
url = ("https://www.libertybooks.com/")
driver.get(url)
driver.implicitly_wait(10)
time.sleep(5)
button = driver.find_element_by_xpath('//a[@class="cc-btn cc-allow"]').click()
Non_fiction = driver.find_element_by_xpath('//ul/li[2][@class="top_level dropdown"]/a').click()
for i in range(30):
multiple_urls = driver.find_element_by_xpath('//img[@class="img-responsive reg-image"]').get_attribute('src')
print(multiple_urls)
Upvotes: 0
Views: 78
Reputation: 1459
The last part of your code needs several modifications:
In:
for i in range(30):
multiple_urls = driver.find_element_by_xpath('//img[@class="img-responsive reg-image"]').get_attribute('src')
print(multiple_urls)
the xpath '//img[@class="img-responsive reg-image"]'
refers to several images, so you should use a function to get the list of theses images, which is "driver.find_elements_by_xpath" and not "driver.find_element_by_xpath" which i used for one element (elements and not element).
so you need to modify that part to:
multiple_urls = driver.find_elements_by_xpath('//img[@class="img-responsive reg-image"]')
now you can get the number of images by:
print(len(multiple_urls))
and, then print the URLs by:
for url in multiple_urls:
print(url.get_attribute('src'))
Upvotes: 1
Reputation: 193298
To count the number of total number of urls you have to Mouse Hover the element with text as Non Fiction inducing WebDriverWait for the visibility_of_all_elements_located()
and you can use the following Locator Strategies:
Code Block:
driver.get("https://www.libertybooks.com/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='cc-btn cc-allow']"))).click()
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='top_level dropdown']//a[contains(., 'Non Fiction')]")))).perform()
print(len(WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//li[@class='top_level dropdown']//a[contains(., 'Non Fiction')]//following::div[1]//ul[contains(@class, 'list-unstyled')]//ul//a")))))
Console Output:
34
Upvotes: 0