python selenium how to deal with absence of an element
<p>So Im trying to translate all the reviews on tripadvisor to save comments(non-translated, original) and translated comments (from portuguese to english). </p>

<p>So the scraper first selects portuguese comments to be displayed , then as usual it converts them into english one by one and saves the translated comments in com_, whereas the expanded non-translated comments in expanded_comments.</p>

<p>The problem now is that for comments which are already in english , there isnt any "Google Translate" widget inside them. But if there isnt any then also I want to atleast save these comments in as English itself. But Im unable to handle the absence of element. </p>

<p>Basically <code>save_comments(driver)</code> function is where its happening.</p>

<pre><code>from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
com_=[]
expanded_comments=[]
date_=[]
driver = webdriver.Chrome("C:\Users\shalini\Downloads\chromedriver_win32\chromedriver.exe")
driver.maximize_window()
from bs4 import BeautifulSoup

def expand_reviews(driver):
    # TRYING TO EXPAND REVIEWS (&amp; CLOSE A POPUP)    
    try:
        driver.find_element_by_class_name("moreLink").click()
    except:
        print "err"
    try:
        driver.find_element_by_class_name("ui_close_x").click()
    except:
        print "err2"
    try:
        driver.find_element_by_class_name("moreLink").click()
    except:
        print "err3"


def save_comments(driver):
    # SELECTING ALL GOOGLE-TRANSLATOR links
    gt= driver.find_elements(By.CSS_SELECTOR,".googleTranslation&gt;.link")

    # NOW PRINTING TRANSLATED COMMENTS
    for i in gt:

        try:
            driver.execute_script("arguments[0].click()",i)

            #com=driver.find_element_by_class_name("ui_overlay").text
            com= driver.find_element_by_xpath(".//span[@class = 'ui_overlay ui_modal ']//div[@class='entry']")
            com_.append(com.text)
            time.sleep(5)
            driver.find_element_by_class_name("ui_close_x").click().perform()
            time.sleep(5)
        except Exception as e:
            pass
    #AS PER user : BREAKS_SOFTWARE
    if gt.size()==0:
        print "ERR"

# ITERATING THROIGH ALL 200 tripadvisor webpages and saving comments &amp; translated comments             
for i in range(56,58):
    page=i*10
    url="https://www.tripadvisor.com/Airline_Review-d8729164-Reviews-Cheap-Flights-or"+str(page)+"-TAP-Portugal#REVIEWS"
    driver.get(url)
    wait = WebDriverWait(driver, 10)
    if i==0:
        # SELECTING PORTUGUESE COMMENTS ONLY # Run for one time then iterate over pages
        try:
            langselction = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.sprite-date_picker-triangle")))
            langselction.click()
            driver.find_element_by_xpath("//div[@class='languageList']//li[normalize-space(.)='Englsih first']").click()
            time.sleep(5)
        except Exception as e:
            print e

    save_comments(driver)
</code></pre>

<pre><code>if (gt.size() == 0): 
    insert code here to extract the english comments
</code></pre>

python selenium web-scraping