Issue
from selenium import webdriver
from selenium.webdriver.common.by import By
try:
url = "https://www.imdb.com/chart/top/"
driver = webdriver.Chrome()
driver.get(url)
title = driver.find_elements(By.XPATH,"//*[(@class = 'ipc-title ipc-title--base ipc-title--title ipc-title-link-no-icon ipc-title--on-textPrimary sc-43986a27-9 gaoUku cli-title')]/a/h3")
year = driver.find_elements(By.XPATH,"//*[(@class = 'sc-43986a27-8 jHYIIK cli-title-metadata-item')][1]")
for i,j in zip(title,year):
print(i.get_attribute("innerHTML"))
print(j.get_attribute("innerHTML"))
except Exception as e:
print(e)
Just wanted to know is there any other way that i can use multiple function in a forloop without using ZIP comman.
Thank you.
Solution
You can iterate over both using index
for i in range(len(title)):
print(title[i].get_attribute("innerHTML"))
print(year[i].get_attribute("innerHTML"))
however I think you should find a common parent and use it to locate the elements you are looking for
items = driver.find_elements(By.CLASS_NAME, 'ipc-metadata-list-summary-item__c')
for item in items:
print(item.find_element(By.CLASS_NAME, 'ipc-title__text').get_attribute('innerHTML'))
print(item.find_element(By.CLASS_NAME, 'cli-title-metadata-item').get_attribute('innerHTML'))
Output
1. The Shawshank Redemption
1994
2. The Godfather
1972
3. The Dark Knight
2008
4. The Godfather Part II
1974
5. 12 Angry Men
1957
...
Answered By - Guy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.