如何在 Python 中使用 Selenium 提取 html 中元素的 href 属性

How to extract href attribute of an element in html with Selenium in Ptyhon

我需要一个图片 URL 列表(之后我会下载它)我不知道如何从 class 中提取元素以及从元素中提取 URLs

driver.get("https://pixabay.com/en/photos/search/" + tag +"/?orientation=horizontal&size=medium")
images = [my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='item']/a")))] 
print("Images on Pixabay were found")

        
for x in images:
    images = driver.find_element(By.XPATH,"/html/body/div[1]/div[2]/div/div[1]/div/div[1]/div/a[1]")
    images = images.get_attribute("href")
    print(images)
    driver.get(images)
    #this is important because if I did not open a URL(Url after open is change to another.) it did not work
    sec_url = driver.current_url
    print( "Sec URL:  " +sec_url)

images 是所有 <href> 属性的列表收集使用:

images = [my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='item']/a")))] 

在列表中迭代时继续前进:

for x in images:

您需要避免使用相同的变量名来标识元素并在循环中存储 <href> 属性。所以需要修改for循环如下:

for x in images:
    element = driver.find_element(By.XPATH,"/html/body/div[1]/div[2]/div/div[1]/div/div[1]/div/a[1]")
    element_href = element.get_attribute("href")
    print(element_href)
    driver.get(element_href)
    sec_url = driver.current_url
    print( "Sec URL:  " +sec_url)