pythonimageseleniumtiktok

Python selenium check if image exists in page


I wanna check if image exist on the page with selenium. I tried different ways but everytime i get an error. Like :

from matplotlib.pyplot import text
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium import webdriver
import time


socialTexts=[]
def sosyalkontrol(text):
    if socialTexts.__contains__(str(text))==True:
        return False
    else:
        socialTexts.append(text)
        return True

username="testUsername"
op=Options()
op.add_experimental_option("debuggerAddress","localhost:8989")
driver= webdriver.Chrome(executable_path="C:\Python\\testProject\\chromedriver.exe",chrome_options=op)
driver.get("http://tiktok.com/@"+username+"/live")


time.sleep(5)

while True:
    for socialmess in driver.find_elements(By.XPATH,('//span[@src="https://p16-webcast.tiktokcdn.com/img/maliva/webcast-va/802a21ae29f9fae5abe3693de9f874bd~tplv-obj.png"]')):
                    print(socialmess.text)

Solution

  • In this code line

    for socialmess in driver.find_elements(By.XPATH,('//span[@src="https://p16-webcast.tiktokcdn.com/img/maliva/webcast-va/802a21ae29f9fae5abe3693de9f874bd~tplv-obj.png"]')):
    

    You are using redundant parenthesis.
    It should be

    for socialmess in driver.find_elements(By.XPATH,'//span[@src="https://p16-webcast.tiktokcdn.com/img/maliva/webcast-va/802a21ae29f9fae5abe3693de9f874bd~tplv-obj.png"]'):
    

    There.
    Also I'm not completely sure you are using a correct locator. I couldn't find any element matching that locator on that page. Not on other similar pages.