获取属性 href returns 通用 link 硒 python
get attribute href returns generic link selenium python
我想得到一个 href link,它应该像这里的 link
<a href="https://www.allmusic.com/album/a-link-in-the-chain-mw0000072661">A Link in the Chain</a>
相反我得到
'https://www.allmusic.com/advanced-search#'
这是代码。我认为问题在于页面是我必须先单击内容才能将其拉起,idk tho。感谢您的帮助
#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
你可以试试Xpath
driver.find_element(By.XPATH,"*The full XPATH here*")
我假设你有一个 div class,它被称为 "title".因此,在这种情况下,您可以轻松使用 xpath 轻松获取该 href。
thing5 = browser.find_element_by_xpath('.//div[@class="title"]/a').get_attribute('href')
我想得到一个 href link,它应该像这里的 link
<a href="https://www.allmusic.com/album/a-link-in-the-chain-mw0000072661">A Link in the Chain</a>
相反我得到
'https://www.allmusic.com/advanced-search#'
这是代码。我认为问题在于页面是我必须先单击内容才能将其拉起,idk tho。感谢您的帮助
#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
你可以试试Xpath
driver.find_element(By.XPATH,"*The full XPATH here*")
我假设你有一个 div class,它被称为 "title".因此,在这种情况下,您可以轻松使用 xpath 轻松获取该 href。
thing5 = browser.find_element_by_xpath('.//div[@class="title"]/a').get_attribute('href')