selenium fails to iterate on elements
<p>Im trying to translate user comments from tripadvisor.<br>
My code :- </p>

<p>1.]Selects only portuguese comments( from language dropdown), </p>

<p>2.]Then expands each of the comments, </p>

<p>3.]Then saves all these expanded comments in a list</p>

<p>4.]Then translates them into english &amp; prints on screen</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_=[] # To save translated comments
expanded_comments=[] # To save expanded comments
driver = webdriver.Chrome("C:\Users\shalini\Downloads\chromedriver_win32\chromedriver.exe")
driver.maximize_window()

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 "err"
    try:
        driver.find_element_by_class_name("moreLink").click()
    except:
        print "err3"




def save_comments(driver):
    expand_reviews(driver)
    # SELECTING ALL EXPANDED COMMENTS
    expanded_com_elements=driver.find_elements_by_class_name("entry")
    time.sleep(3)
    for i in expanded_com_elements:
        expanded_comments.append(i.text)


    # 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)
            #i.click().perform()
            com=driver.find_element_by_class_name("ui_overlay").text
            com_.append(com)
            time.sleep(5)
            driver.find_element_by_class_name("ui_close_x").click().perform()
            time.sleep(5)
        except Exception as e:
            pass
            #print e



for i in range(282):
    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(.)='Portuguese first']").click()
            time.sleep(5)
        except Exception as e:
            print e

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

<p>================ERROR=================</p>

<p>expanded_comments return empty list. Some comments get saved, some get skipped.
First page is saved properly (all comments expanded), but thereafter only first comment gets saved, without being expanded. But translated comments from all pages get saved properly in com_</p>

<p>I have changed your code and now it's working.</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

driver = webdriver.Chrome("./chromedriver.exe")
driver.maximize_window()
url="https://www.tripadvisor.com/Airline_Review-d8729164-Reviews-Cheap-Flights-TAP-Portugal#REVIEWS"
driver.get(url)
wait = WebDriverWait(driver, 10)



# SELECTING PORTUGUESE COMMENTS ONLY
#show_lan = driver.find_element_by_xpath("//div[@class='languageList']/ul/li[contains(text(),'Portuguese first')]")

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(.)='Portuguese first']").click()
    time.sleep(5)
except Exception as e:
    print e


# 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 "err"
try:
    driver.find_element_by_class_name("moreLink").click()
except:
    print "err3"

# SELECTING ALL EXPANDED COMMENTS
expanded_com_elements=driver.find_elements_by_class_name("entry")
expanded_comments=[]
time.sleep(3)
for i in expanded_com_elements:
    expanded_comments.append(i.text)

# 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)
        #i.click().perform()
        print driver.find_element_by_class_name("ui_overlay").text
        time.sleep(5)
        driver.find_element_by_class_name("ui_close_x").click().perform()
        time.sleep(5)
    except Exception as e:
        pass
        #print e
</code></pre>

python selenium web-scraping