当前位置: 首页 > 知识库问答 >
问题:

返回元素后如何正确关闭Selinium web驱动程序?

王云
2023-03-14

我有以下selenium脚本,等待页面加载并查找元素。在检索到元素之后,我想关闭驱动程序,这样我的ram就会被释放,但是整个程序以invalidsessionidexception结束,这意味着驱动程序在不适当的时间被关闭。我如何正确关闭我的驱动程序后,我想要的信息?

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait



def selenium_get_time(ort):
    options = Options()
    options.headless = True
    driver = webdriver.Chrome(chrome_options=options, executable_path='/Users/andreas/.wdm/chromedriver/83.0.4103.39/mac64/chromedriver')

    driver.get("https://fp.trafikverket.se/boka/#/search/dIccADaISRCIi/5/0/0/0")
    element = WebDriverWait(driver, 40).until(EC.element_to_be_clickable((By.CLASS_NAME, "form-control")))
    driver.find_element_by_xpath("//select[@id='examination-type-select']/option[@value='3']").click()
    driver.find_element_by_xpath("//select[@id='language-select']/option[@value='13']").click()
    driver.find_element_by_id('id-control-searchText').clear()
    inputElement = driver.find_element_by_id("id-control-searchText")
    inputElement.send_keys(ort)
    inputElement.send_keys(Keys.ENTER)
    # time.sleep(10)
    try:
        element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='col-sm-3 text-center']/button[@data-bind='click:$parent.select']")))
        first_time = driver.find_element_by_xpath("//div[@class='col-xs-6']/strong")
        return first_time.text
    except (NoSuchElementException, TimeoutException) as e:
        if NoSuchElementException:
            print('Nothing found for: ', ort, ' NoElemFound')
        else:
            print('Nothing found for: ', ort, ' TimedOut')
    finally;
        driver.close()
        driver.quit()

#This is the program that I run
def main(ort):
    first_availible = selenium_get_time(ort)
    if first_availible:
        date = convert_time(first_availible)
        if check_schedule(date, '2020-07-01', '2020-07-05'):
            print('FOUND: ', ort +' '+ first_availible)
            send_email(first_availible, ort)
        else:
            now = datetime.datetime.now()
            dt_string = now.strftime("%H:%M:%S")
            print('Found Nothing for: ', ort, ' ', dt_string)


共有1个答案

卞俊贤
2023-03-14

当您呼叫时:

first_availible = selenium_get_time(ort)

def selenium_get_time(ort)中,您正在调用:

finally:
    driver.close()
    driver.quit()

因此,当控件返回到main(ort)时,您看到的是invalidsessionidexception

from selenium import webdriver

driver = None

def selenium_get_time(ort):
    global driver
    options = Options()
    options.headless = True
    driver = webdriver.Chrome(chrome_options=options, executable_path='/Users/andreas/.wdm/chromedriver/83.0.4103.39/mac64/chromedriver')
    .
    .
#This is the program that I run
def main(ort):
    first_availible = selenium_get_time(ort)
    .
    .
    tear_down()

def tear_down():
    driver.quit()

您可以在Selenium.common.exceptions.invalidsessionidexception中找到相关的讨论,使用GeckoDriver Selenium Firefox在无头模式下通过Python

 类似资料:
  • 我在一个开发商业智能(报告)工具的团队中。我们报告了许多来源,包括存储过程。我们使用JDBC驱动程序提供的元数据来确定存储过程的输入和输出参数。 PostgreSQL 9 JDBC驱动程序似乎错误地返回了过程参数的元数据。 例如,我的存储过程如下所示: 所以它在一个结果集中有一个参数,两列返回。 PostgreSQL驱动程序报告有3个IN参数。 personid(参数) 人员(返回第一列) 名称(

  • 我对使用Python的Selenium是新手。我正在尝试获取一些数据,但我不知道如何解析来自以下命令的输出: 我试图在谷歌上搜索一些教程,但我没有找到Python的任何内容。 你能给我个提示吗?

  • 问题内容: 我有全部传播异常的方法,然后在一个地方处理,但是我意识到了一些事情。 假设我有这样的方法 我的问题是,如果doSometing()方法引发异常,该语句将不会关闭,但我不想在那里处理异常。尝试并捕获只会抛出异常并最终关闭语句的正确方法吗? 问题答案:

  • 问题内容: 在清理一些代码时,FindBugs向我介绍了一些使用Connection,CallableStatement和ResultSet对象的JDBC代码。这是该代码的一个片段: FindBugs指出这些应该在finally块内。我开始重构我的代码来做到这一点,我开始想知道如何在finally块中处理代码。 Connection对象的CallableStatement的创建可能会引发异常,而我

  • 问题内容: 其中哪一个是正确的? 问题答案: 工作正常,并正确关闭标签。最好为视障人士添加属性。

  • 问题内容: 如何正确关闭IPython Notebook? 目前,我只是关闭浏览器选项卡,然后在终端中使用。 不幸的是,滴答也无济于事(它们确实杀死了它们的内核,但没有退出iPython)。 问题答案: 当前没有比终端中的Ctrl + C更好的方法了。 我们正在考虑如何进行显式关机,但是笔记本作为单用户应用程序(用户可以自由停止它)和作为多用户服务器(只能由管理员操作)之间存在一些紧张关系。阻止它