【问题标题】:python - closing firefox browser down when completepython - 完成后关闭Firefox浏览器
【发布时间】:2016-04-11 16:14:21
【问题描述】:

我在以下代码上使用 driver.close() 在代码完成后关闭 firefox 浏览器,但在第一个循环后出现错误。我使用正确吗?

错误:

ConnectionRefusedError: [WinError 10061] 无法建立连接 因为目标机器主动拒绝了它

代码:

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()

【问题讨论】:

  • 看起来您只打开一次驱动程序,然后在第一个循环后关闭它。因此,当您再次尝试使用它时,它已关闭
  • 另外,函数应该是driver.quit()

标签: python firefox


【解决方案1】:

您对driver.close()(应该是driver.quit())的呼叫完成得太早了。

你需要在你所有的工作完成之后,所以在你的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()

【讨论】:

    猜你喜欢
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 2018-10-20
    相关资源
    最近更新 更多