Search code examples
pythonhtmlseleniumselenium-webdriverhref

get attribute href returns generic link selenium python


I want to get a href link, it should be something like the link here

<a href="https://www.allmusic.com/album/a-link-in-the-chain-mw0000072661">A Link in the Chain</a>

instead I get

'https://www.allmusic.com/advanced-search#'

Heres the code. I think the problem is that the page is that I gotta click stuff first to pull it up, idk tho. Thxs for any help

#This gets every album in the page
browser = webdriver.Chrome('c:\\Users\\16308\\Documents\\VSCPython\chromedriver')
def get_page(pge_url):
    browser.get(pge_url)
    time.sleep(1)
    add_stuff(pge_url)
    time.sleep(2)
    thing1 = browser.find_element_by_class_name('content-container')
    time.sleep(4)
    thing2 = thing1.find_element_by_class_name('content')
    time.sleep(8)
    thing3 = thing2.find_element_by_class_name('results')
    time.sleep(4)
    thing4 = thing3.find_element_by_class_name('desktop-results')
    time.sleep(4)
    thing5 = browser.find_element_by_class_name('title').find_element_by_css_selector('a').get_attribute('href')
    print("5: ",thing5)
    return thing5

get_page('https://www.allmusic.com/advanced-search')

#clicks range of years
def add_stuff(current_page):
    time.sleep(1)
    browser.get(current_page)
    time.sleep(2)
    selectOne = Select(browser.find_element_by_class_name('start-year'))
    time.sleep(4)
    selectOne.select_by_visible_text('1920')
    time.sleep(8)
    selectTwo = Select(browser.find_element_by_class_name('end-year'))
    time.sleep(8)
    selectTwo.select_by_visible_text('2022')
#IMPORT STATEMENTS
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.support import expected_conditions 
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import Select
import dns
import pymongo 
import sys
from pymongo import MongoClient

Solution

  • I am assuming that you have a div for that class which is called as "title". So in that case, you can easily use xpath to get that href easily.

    thing5 = browser.find_element_by_xpath('.//div[@class="title"]/a').get_attribute('href')