【发布时间】:2018-09-30 07:27:46
【问题描述】:
我正在尝试使用命令提示符运行为 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 文件保存在其他文件位置。所以我尝试以不同的方式运行文件 我是这样跑的:
> C:\Users\mypath\Python37\python.exe "C:\Users\mypath\PythonScripts\SeleniumPractice.py"
我收到此错误消息"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 创建脚本以来,我在每一行都有 >>>,我将其替换为空白并注释掉从 IDLE 生成的任何其他行,现在我可以使用 python .py 下面提供的相同解决方案运行脚本.谢谢大家
【问题讨论】:
-
好的,所以你已经运行了 Python 解释器,它正在尝试运行你的脚本。但是脚本中存在语法错误,因此它会打印该错误消息。该错误消息意味着您在某行的字符串外有一个反斜杠“\”,后面有一个或多个额外字符。但是,在您发布的代码中,没有这样的反斜杠。因此,请检查您硬盘上的代码是否与您发布的代码完全相同相同。
-
如果您发布完整的回溯会有所帮助,以便我们可以看到哪一行给您带来了问题。
标签: python python-3.x cmd