我正在尝试使用selenium和python获取Chrome扩展程序评论者的Google+网址链接。我使用的代码如下:
from selenium import webdriver
from selenium.common.exceptions import ElementNotVisibleException, \
NoSuchElementException
import sys
import time
import traceback
def create_browser(first_page=None):
#print "Starting"
browser = webdriver.Chrome('/home/user/ChromeDriver/chromedriver')
if first_page:
browser.get(first_page);
#print "Done."
return browser
def close_browser(browser):
browser.close()
def wait_find_elements_by_class_name(driver, name):
counter = 0
while counter < 7:
try:
elem = driver.find_elements_by_class_name(name)
break
except NoSuchElementException:
time.sleep(1)
counter += 1
elem = None
return elem
def wait_find_element_by_xpath(driver, path):
counter = 0
while counter < 7:
try:
elem = driver.find_element_by_xpath(path)
break
except NoSuchElementException:
time.sleep(1)
counter += 1
elem = None
return elem
browser = create_browser()
#print 'loaded'
try:
#print "Fetching URL"
url = 'https://chrome.google.com/webstore/detail/movies-showtimes/aaaagimjfioddedkjmggbabjgblfdnkd/reviews'
print url
browser.get(url)
while(1):
passJS = 0
gPlusLinks = wait_find_elements_by_class_name(browser, 'comment-thread-displayname')
#print "Bla"
for link in gPlusLinks:
passJS += 1
if passJS <= 2:
continue
l = link.get_attribute('href')
if l != "javascript:void(0)":
print l
'''time.sleep(3)
try:
a = wait_find_element_by_xpath(browser, './/a[@class="webstore-eh webstore-ib-Vg"]')
a.click()
except ElementNotVisibleException:
break
except AttributeError:
break
print "Scrolled and clicked."
time.sleep(3) '''
except:
print "'For loop' exception: ", sys.exc_info()[0]
for tb in traceback.format_tb(sys.exc_info()[2]):
print tb
print "Done"
我的问题是,当我将第61行的评论部分(time.sleep(3)
)取消注释到第70行的time.sleep(3)
时,我的程序终止而不打印Google+网址(即print l
声明)。但是,在评论该部分后,它会正确打印URL。
任何人都可以解释为什么会发生这种情况,我该如何预防呢?我真的需要代码才能按顺序执行。我能理解代码是多线程的,但事实并非如此。所以我不知道如何解释这个。