I'm trying to use a function from the webdriver library in an if condition, but this condition is situational, that is, it doesn't always happen. Obviously it gives me an error because the times that the condition is not true, the function "driver.find_element_by_css_selector()" does not find the element. Is there a way to do it so that my code doesn't crash?
if driver.find_element_by_css_selector("#react-root > div > div > section > main > div > div > div > div > a").text() == 'Volver a Instagram.':
return 0
else:
if(p == 1):
elementName1 = driver.find_elements_by_class_name("tWeCl")
src_url = elementName1[0].get_attribute("src")
if(p == 2):
elementName2 = driver.find_elements_by_class_name("tWeCl")
src_url = elementName2[0].get_attribute("src")
try, except
block.