如何使用 Selenium Webdriver 在 gipfy.com 上查找元素

How to find elements on gipfy.com using Selenium Webdriver

我不知道为什么,但如果我使用 selenium,它现在总是不起作用。 giphy.com 我有问题。这是我的代码

tag = "pixelart"
driver.get(f"https://giphy.com/search/{tag}")
time.sleep(2)
gifs = driver.find_elements(By.XPATH,"/html/body/div[4]/div[1]/div/div[6]/div[2]/div[1]")
print(gifs)
for gif in gifs:
    href = gif.get_attribute("href")
    print(f"Test : {href})

输出应该是 URL in href:

试试这个

elems = driver.find_elements_by_xpath(
          "//div[@class="giphy-grid"]//child::div//child::a"
       )

urls = [url for url in elems.get_attribute("href")]

您也可以尝试使用 WebDriverWait 等待元素加载,而不是使用 time.sleep 函数。

您使用过的

/html/body/div[4]/div[1]/div/div[6]/div[2]/div[1]

仅标识 HTML DOM 中的一个元素,因此您只能看到一个元素。

要提取 href 属性的值,您必须归纳 WebDriverWait for visibility_of_all_elements_located() and you can use either of the following :

  • 使用 XPATH:

    driver.get("https://giphy.com/search/pixelart")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Agree and close']"))).click()
    print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//picture//ancestor::a[1][@href]")))])
    
  • 控制台输出:

    ['https://giphy.com/gifs/party-epic-fest-26AHzeyITON4vzMM8', 'https://giphy.com/gifs/2tTh7wB6DtL1QQvAF5', 'https://giphy.com/gifs/perfect-loops-pVGsAWjzvXcZW4ZBTE', 'https://giphy.com/gifs/bw-pixelart-minimalism-3oEdvc7q2VwJFEQDGU', 'https://giphy.com/gifs/hoppip-heart-hoppip-pixel-BXVRf5GyMlElO', 'https://giphy.com/gifs/1yld7nW3oQ2IyRubUm', 'https://giphy.com/gifs/breakfast-bacon-egg-3oEdv9R4D62GPrVY4g', 'https://giphy.com/gifs/80s-synthwave-aesthetic-84SFZf1BKgzeny1WxQ', 'https://giphy.com/gifs/hoppip-heart-hoppip-pixel-1S9kD6xm4601O', 'https://giphy.com/gifs/xWMPYx55WNhX136T0V', 'https://giphy.com/gifs/perfect-loops-9LZTcawH3mc8V2oUqk', 'https://giphy.com/gifs/art-pixel-TRebCjNbc4dIA', 'https://giphy.com/gifs/80s-synthwave-aesthetic-k81NasbqkKA5HSyJxN', 'https://giphy.com/gifs/pixel-art-scenery-pI43YlhMoPqsE', 'https://giphy.com/gifs/pixel-sky-pixelart-2wh8ugh52dGSJYrA26', 'https://giphy.com/gifs/art-pixel-rzeWnbH8Uc5Y4', 'https://giphy.com/gifs/S5uMJDmtnATLbjjw3h', 'https://giphy.com/gifs/earth-spinning-globe-l3V0megwbBeETMgZa', 'https://giphy.com/gifs/pixel-netflix-art-26hisvCylQN7VcaOI', 'https://giphy.com/gifs/pixel-art-10GVNnqO2ZoAh2', 'https://giphy.com/gifs/art-pixel-marvel-NPd2pkbsjftS3O3U9c', 'https://giphy.com/gifs/OpCIhPH16jzsL3IzRp', 'https://giphy.com/gifs/animation-nes-pixelart-26tn84fF0eL3c898c', 'https://giphy.com/gifs/pixels-16bit-picel-art-3o85xunRezGKPOkcG4', 'https://giphy.com/gifs/pixel-art-octobit-pixeltober-l3vRgqJIdbRp7Exfa']