我需要一個圖片的 URL 串列(之后我會下載它)我不知道如何從類中提取元素和從元素中提取 URL
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)
uj5u.com熱心網友回復:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/457808.html
