Getting src link to XKCD image?

I am trying to get the src link (URL) of the main image from xkcd.com. I use the following code, but it returns something like this -> session = "2f69dd2e-b377-4d1f-9779-16dad1965b81", element = "{ca4e825a-88d4-48d3-a564-783f9f976c6b}"

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()

browser.get('http://xkcd.com')
assert 'xkcd' in browser.title

idlink= browser.find_element_by_id("comic")

#link = idlink.get_attribute("src") ## print link prints null

print idlink

using the xpath method also returns the same as above. Please help me get the image url.

+4
source share
2 answers

browser.find_element_by_idreturns the web element, and that is what you print. In addition, the desired text is in the child element idlink. Try

idlink = browser.find_element_by_css_selector("#comic > img")
print idlink.get_attribute("src")

idlink - - img, comic. URL- src, .

+2

:

  • img ( div)
  • img

    img_tag = browser.find_element_by_xpath("//div[@id='comic']/img")
    print img_tag.get_attribute("src")
    

URL-

selenium python

XPath Selenium .

+2

Source: https://habr.com/ru/post/1624322/


All Articles