2016-04-11 21 views
0

次のコードでdriver.close()を使用して、コードが終了したらFirefoxを終了しますが、最初のループの後にエラーがスローされます。私はそれを正しく使用していますか?python - 完了時にfirefoxブラウザを閉じる

エラー:

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

コード:

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

ドライバを一度開いてから最初のループの後に閉じているようです。だから、もう一度それを使用しようとすると閉じます – fdsa

+2

また、関数は['driver.quit()'](http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_firefox/selenium)にする必要があります。 .webdriver.firefox.webdriver.html#selenium.webdriver.firefox.webdriver.WebDriver.quit) – Cyrbil

答えて

1

driver.quit()する必要があります)driver.close()へのあなたのコールがあまりにも早く行われます。

すべての作業が完了した後に置く必要があるので、forの後に入力してください。

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() 
関連する問題