我能夠從 html 中提取名稱,但是嘗試獲取 href 鏈接不起作用。我需要得到它,所以它也會在其附加的名稱旁邊列印鏈接。在我能做到這一點之前,我首先需要能夠列印鏈接。
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
user_input = input("character: ")
options = Options()
options.headless = True
options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe")
driver.get("https://www.marvel.com/search")
search_input_xpath = "//input[@placeholder='Search']"
search_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, search_input_xpath)))
search_input.send_keys(user_input)
first_item_in_auto_suggest_area_xpath = "//div[contains(@id,'react-autowhatever')]/ul"
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, first_item_in_auto_suggest_area_xpath)))
search_input.send_keys(Keys.ENTER)
section = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".search-list p.card-body__headline a")))
character = driver.find_elements(By.CSS_SELECTOR, ".search-list p.card-body__headline a")
for character_tag in character:
print(character_tag.text)
href_elements = driver.find_elements(By.CSS_SELECTOR, ".search-list p.card-body__headline href")
for href in href_elements:
print(href.get_attribute("href"))
uj5u.com熱心網友回復:
您無法使用 css 選擇器訪問屬性。
我會先嘗試更改href為a:
href_elements = driver.find_elements(By.CSS_SELECTOR, ".search-list p.card-body__headline a")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/496223.html
