如何在Windows命令提示符下运行.py文件?

2024-09-30 01:28:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用命令提示符运行为Selenium创建的文件,但是我无法找出问题所在,因为我遵循了这里和Google中提供的一些解决方案,但是我收到了相同的错误消息。在

当我尝试一个接一个地运行这段代码时:我可以运行它而没有任何错误,并成功登录

代码如下:

  import selenium
  from selenium import webdriver
  from selenium.webdriver.common.by import By
  from selenium.webdriver.support.ui import Select
  from selenium.common.exceptions import NoSuchElementException
  baseurl = "http://www.gcrit.com/build3/admin/"
  username = "admin"
  password = "admin@123"
  xpaths = {'usernameTxtBox': '//input[@name="username"]', 'passwordTxtBox': '//input[@name="password"]', 'loginButton': '//button[@id="tdb1"]'}
  mydriver = webdriver.Chrome(executable_path=r"C:\mypath\Forselenium\chromedriver.exe")
  mydriver.get(baseurl)
  mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).send_keys(username)   
     mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).send_keys(password)
  mydriver.find_element_by_xpath(xpaths['loginButton']).click()

我将其保存在.py文件中,然后在环境变量"C:\mypath\Local\Programs\Python\Python37"中添加python37的扩展名 并在路径中添加了.py。在

我的python文件保存在其他文件位置。所以我试着用不同的方式运行文件 我是这样跑的:

^{pr2}$

我收到这个错误消息"SyntaxError: unexpected character after line continuation character"

另外,我试着用这个链接how to run .py files in command prompt (Windows 7)中提到的方式运行,但是它仍然不能正常工作。在

  I tried some of the solutions provided below as well but I am receiving error for some reason: 
 I changed the directory as well, but it is saying no such file but the file (SeleniumPractice.py)  exists in this path. 
    C:\Users\Desktop\Learning\PythonScripts>py SeleniumPractice (tried using .py as well but receiving error like invalid syntax) 
   (null): can't open file 'SeleniumPractice': [Errno 2] No such file or directory

我也尝试使用下面提供的其他解决方案,但是收到了这个错误。在

   I am using this command C:\Users\>python SeleniumPractice.py "File 
  "SeleniumPractice.py", line 1 Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 
   2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32 ^ SyntaxError: 
   invalid syntax"

因为我是从IDLE创建脚本的,所以我在每一行上都有>>>的内容,我将其替换为blank,并注释掉从IDLE生成的任何其他行,现在我可以使用python.py下面提供的相同解决方案来运行脚本。谢谢大家


Tags: 文件frompyimportbyadminselenium错误
3条回答

在命令提示符中转到该位置 那么

python <filename.py>

只需执行

^{pr2}$

命令提示符中的命令。如果出现错误,则环境变量设置不正确 但现在python的新版本通常不是这样,因为它是自动生成的;也就是说,在安装时自己创建条目。在

我认为你的问题有困难,因为你犯了一大堆错误

  1. 请注意,正如您所写:

I received this error message "SyntaxError: unexpected character after line continuation character"

我们可以假设您实际上可以从命令提示符运行.py文件。你只是有语法错误。 从阅读代码中,我看到在最后一行的旁边有一个多余的标签,这可能会导致错误。在

2.你写的

C:\Users\Desktop\Learning\PythonScripts>py SeleniumPractice (tried using .py as well but receiving error like invalid syntax) (null): can't open file 'SeleniumPractice': [Errno 2] No such file or directory

意思是你得到了一个路径错误。这也是一个python错误,告诉您在当前路径(即C:\Users\Desktop\Learning\PythonScripts)中找不到“SeleniumPractice”文件。它真的位于那里吗?在

  1. 你写道:

I am using this command C:\Users>python SeleniumPractice.py "File "SeleniumPractice.py", line 1 Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32 ^ SyntaxError: invalid syntax"

但我们不能准确地知道你的语法错误。它只说明代码的第1行有语法错误。试着从空闲状态运行它,并给我们完整的错误描述?在

正如其中一条评论中所写的,pycharm可能真的有助于你更好地理解错误的机制。或者在list中学习一点如何使用命令提示符,因为,例如,如果在环境变量中正确地安装了PATH,则不需要编写完整的路径python.exe位置,只需要写“python”然后写下你要运行的文件的相对路径,它就会运行得很漂亮。在

您对python似乎很陌生,我建议您使用像pycharm这样的IDE。它就像一个符咒

相关问题 更多 >

    热门问题