Home > Software design >  How to find elements on gipfy.com using Selenium Webdriver
How to find elements on gipfy.com using Selenium Webdriver

Time:05-02

I do not know why but always if I am using selenium it is not working now. I have a problem with a giphy.com. Here is my code

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})

Output should be a URL in href:

CodePudding user response:

The locator strategy you have used:

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

identifies only one element within the HTML DOM, hence you see only a single element.

To extract the value of the href attributes you have to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following locator strategies:

  • Using 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]")))])
    
  • Console Output:

    ['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']
    

CodePudding user response:

try this

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

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

you can also try WebDriverWait to wait until element is loaded instead use time.sleep function.

  • Related