Python selenium function return True If image exists in page

Question:

I’m trying to do something with live streams on social media and when someone send a gift to streamer I want to know what gift is that so I need a function which is returns True when the selected gift .png exists in page.

That code part which is I select as a solution work perfectly.

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.google.com/')

def checkString(yourstring):
    if yourstring in driver.page_source:
        return True
    else:
        return False

print(checkString('googlelogo_color_272x92dp.png'))
Asked By: Kerem

||

Answers:

How about Beautifulsoup?

parser = BeautifulSoup(driver.page_source, "html.parser")

and then call find_all, if you have an example I can be more specific 🙂

Answered By: piscoony

try:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.google.com/')

def checkString(yourstring):
    if yourstring in driver.page_source:
        return True
    else:
        return False

print(checkString('googlelogo_color_272x92dp.png'))
Answered By: sysus

How about this xpath?

'//img[contains(@src, ".png")]'
Answered By: piscoony
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.