2016-04-11 20 views
0

Kod bittiğinde firefox tarayıcısını kapatmak için aşağıdaki kodda driver.close() kullanıyorum ancak ilk döngüden sonra bir hata oluştu. Doğru kullanıyor muyum?python - tamamlandığında firefox tarayıcısını kapatılıyor

hatası:

ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

kodu: driver.close() için

driver = webdriver.Firefox() 
print('Firefox started') 
print('Iterating links') 
connection = pymysql.connect(host='.com', user='', password='', db='', cursorclass=pymysql.cursors.DictCursor) 
with connection.cursor() as cursor2: 
    cursor2.execute("Delete from todaysmarkets") 
for link in links: 
    try: 
     print('Fetching from link: ' + base + link) 
     driver.get(base+link) 
     print('Waiting for all the data to get loaded') 
     element = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CLASS_NAME,"current"))) 
     print('Parsing page') 
     tree = html.fromstring(driver.page_source) 
     names = tree.xpath('//*[@id="lpTable1"]/div[h3[text()=" Goal Supremacy and Goal Markets "]]/div/table/tbody/tr[not(@style="display: none;")]/td/span/text()') 
     print(str(len(names)) + ' markets found') 
     sells = tree.xpath('//*[@id="lpTable1"]/div[h3[text()=" Goal Supremacy and Goal Markets "]]/div/table/tbody/tr[not(@style="display: none;")]/td[3]/button/text()') 
     buys = tree.xpath('//*[@id="lpTable1"]/div[h3[text()=" Goal Supremacy and Goal Markets "]]/div/table/tbody/tr[not(@style="display: none;")]/td[4]/button/text()') 
     if(len(buys) != len(sells) or (len(buys) == 0 or len(sells) == 0)): 
      print('Error fetching markets either suspended or does not exist') 
      continue 
     print('Putting markets into excel file') 
     with connection.cursor() as cursor: 
      for i in range(0, len(names)): 
       cursor.execute(("INSERT INTO todaysmarkets(URL,Name,value) VALUES(%s,%s,%s)"), (base+link,names[i], str((float(sells[i])+float(buys[i]))/2.0))) 



      # ws.append([names[i], str((float(sells[i])+float(buys[i]))/2.0)]) 
    finally: 
     print('Saving the file with name markets.xlsx') 
     driver.close() 
+0

Yalnızca sürücüyü bir kez açıp ilk döngüden sonra kapattığınız anlaşılıyor. Yani tekrar kullanmaya çalıştığınızda kapalı – fdsa

+2

kapalı Ayrıca, işlev ['driver.quit()'] olmalıdır (http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_firefox/selenium .webdriver.firefox.webdriver.html # selenium.webdriver.firefox.webdriver.WebDriver.quit) – Cyrbil

cevap

1

Çağrı (driver.quit() olmalıdır) çok yakında yapılır.

Tüm çalışmalarınız bittikten sonra, for öğesinden sonra koymanız gerekir.

driver = webdriver.Firefox() 
print('Firefox started') 
print('Iterating links') 
connection = pymysql.connect(host='.com', user='', password='', db='', cursorclass=pymysql.cursors.DictCursor) 
with connection.cursor() as cursor2: 
    cursor2.execute("Delete from todaysmarkets") 
for link in links: 
    try: 
     print('Fetching from link: ' + base + link) 
     driver.get(base+link) 
     print('Waiting for all the data to get loaded') 
     element = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CLASS_NAME,"current"))) 
     print('Parsing page') 
     tree = html.fromstring(driver.page_source) 
     names = tree.xpath('//*[@id="lpTable1"]/div[h3[text()=" Goal Supremacy and Goal Markets "]]/div/table/tbody/tr[not(@style="display: none;")]/td/span/text()') 
     print(str(len(names)) + ' markets found') 
     sells = tree.xpath('//*[@id="lpTable1"]/div[h3[text()=" Goal Supremacy and Goal Markets "]]/div/table/tbody/tr[not(@style="display: none;")]/td[3]/button/text()') 
     buys = tree.xpath('//*[@id="lpTable1"]/div[h3[text()=" Goal Supremacy and Goal Markets "]]/div/table/tbody/tr[not(@style="display: none;")]/td[4]/button/text()') 
     if(len(buys) != len(sells) or (len(buys) == 0 or len(sells) == 0)): 
      print('Error fetching markets either suspended or does not exist') 
      continue 
     print('Putting markets into excel file') 
     with connection.cursor() as cursor: 
      for i in range(0, len(names)): 
       cursor.execute(("INSERT INTO todaysmarkets(URL,Name,value) VALUES(%s,%s,%s)"), (base+link,names[i], str((float(sells[i])+float(buys[i]))/2.0))) 



      # ws.append([names[i], str((float(sells[i])+float(buys[i]))/2.0)]) 
    finally: 
     print('Saving the file with name markets.xlsx') 

# Close driver at the end of the work 
driver.quit() 
İlgili konular