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.