1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] VS Code not running Python def functions [closed]

Discussão em 'Python' iniciado por Stack, Setembro 11, 2024.

  1. Stack

    Stack Membro Participativo

    I installed VS Code, Python and Python extensions

    In VS Code, when I don’t use def functions my code works as expected in the Explorer & Debug and Run areas and messages get printed as expected to terminal. As expected, the code doesn’t appear in the Testing area to run.

    code without def

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.chrome.options import Options

    chrome_options = Options()
    chrome_options.add_argument("--disable-search-engine-choice-screen")
    driver = webdriver.Chrome(options=chrome_options)
    driver.implicitly_wait(5)

    driver.get("https://www.amazon.co.uk/")
    search = driver.find_element(By.ID,'twotabsearchtextbox')
    search.send_keys("dress", Keys.ENTER)

    expected_text = '"dress"'
    print('Found', expected_text)
    actual_text = driver.find_element(By.XPATH, "//span[@class='a-color-state a-text-bold']").text

    assert expected_text == actual_text, f'Error. Expected text: {expected_text}, but actual text: {actual_text}'
    driver.quit()


    So, if I add def functions to my code, the code runs in Testing (but I don’t see any of the print outputs listed in the code).

    code with def

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.chrome.options import Options

    def test_amazon_search_dress():

    chrome_options = Options()
    chrome_options.add_argument("--disable-search-engine-choice-screen")
    driver = webdriver.Chrome(options=chrome_options)
    driver.implicitly_wait(5)

    driver.get("https://www.amazon.co.uk/")
    search = driver.find_element(By.ID,'twotabsearchtextbox')
    search.send_keys("dress", Keys.ENTER)

    expected_text = '"dress"'
    print('Found', expected_text)
    actual_text = driver.find_element(By.XPATH, "//span[@class='a-color-state a-text-bold']").text

    assert expected_text == actual_text, f'Error. Expected text: {expected_text}, but actual text: {actual_text}'
    driver.quit()


    The only difference between the two code snippets is def test_amazon_search_dress():

    The big issue is that code with def doesn’t run for me in the Explorer & Debug and Run, unless I follow the answer to my issue shown here VS Code not accepting Python Methods , which worked, but I noticed from video https://www.youtube.com/watch?v=D2cwvpJSBX4&t=273s @8:33 that when def code runs from Explorer, Debug and Run & Testing and prints messages without the solution suggested in stackoverflow.

    Is there any advice or help you can give me please as I'm not sure if the issue is VS Code or my code?

    Note: I completed set-up before watching video, so I didn’t create the .venv (I’m using Global/Microsoft Store) and I didn’t do the discord step.

    Continue reading...

Compartilhe esta Página