Home > other >  Python Selenium - fetching titles and links in a loop
Python Selenium - fetching titles and links in a loop

Time:05-20

I have trying to do this using Selenium in Python:

title1

link1

title2

link2 ...

Currently I have this code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

PATH = r"C:\Users\Desktop\py\msedgedriver.exe"
driver = webdriver.Edge(PATH)

driver.maximize_window()
driver.get('https://www.google.com/')
searchbar = driver.find_element(by=By.CLASS_NAME, value='gLFyf')
searchbar.send_keys('selenium')
searchbar.send_keys(Keys.RETURN)
titles = driver.find_elements(by=By.CLASS_NAME, value='LC20lb')
links = driver.find_elements(by=By.TAG_NAME, value='a')
for link in links:
    href = link.get_attribute('href')
    print(href)
for title in titles:
    print(title.text)

time.sleep(5)
driver.quit()

However, the links printed out are Google search links, instead of the links of the websites themselves. Also, all the links are printed out before the titles (I understand why this happens but don't know how to fix that)

May I ask what are the ways to solve the 2 problems? Thank you in advance.

CodePudding user response:

Replace the for loop in your code with,

for i, link in enumerate(links):
    try:
        print(titles[i].text)
    except:
        pass
    print(link.get_attribute("href"));print()

Output -

Selenium Tutorial for Beginners: Learn WebDriver & Testing
https://www.google.com/search?q=selenium&source=lnms&tbm=bks&sa=X&ved=2ahUKEwiHuNKqiOv3AhXITWwGHZXxBlwQ_AUoAXoECAIQAw

Selenium: Definition, How it works and Why you need it
https://www.google.com/search?q=selenium&source=lnms&tbm=isch&sa=X&ved=2ahUKEwiHuNKqiOv3AhXITWwGHZXxBlwQ_AUoAnoECAIQBA

What Is Selenium � A Tutorial on How to Use ... - LambdaTest
https://www.google.com/search?q=selenium&source=lnms&tbm=vid&sa=X&ved=2ahUKEwiHuNKqiOv3AhXITWwGHZXxBlwQ_AUoA3oECAIQBQ
  • Related