Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Selenium中,如何在登录网站时处理InvalidSelectorException_Javascript_Python_Selenium - Fatal编程技术网

Javascript 在Selenium中,如何在登录网站时处理InvalidSelectorException

Javascript 在Selenium中,如何在登录网站时处理InvalidSelectorException,javascript,python,selenium,Javascript,Python,Selenium,我正在尝试使用selenium登录网站。这是我写的代码 from selenium import webdriver driver = webdriver.Chrome() driver.get("https://abcde.com") assert "xxx" in driver.title user = driver.find_element_by_css_selector("#ctl00_MasterHeaderPlaceHolder_ctl00_userNameTextbox") u

我正在尝试使用selenium登录网站。这是我写的代码

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://abcde.com")

assert "xxx" in driver.title
user = driver.find_element_by_css_selector("#ctl00_MasterHeaderPlaceHolder_ctl00_userNameTextbox")
user.clear()
user.send_keys("username")

pwd = driver.find_element_by_css_selector("#ctl00$MasterHeaderPlaceHolder$ctl00$passwordTextbox")
pwd.clear()
pwd.send_keys("mypassword")
pwd.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()
当我运行此代码时,出现了InvalidSelectorException

引发异常类(消息、屏幕、堆栈跟踪) selenium.common.exceptions.InvalidSelectorException:消息:无效选择器:指定的选择器无效或非法 (会话信息:chrome=59.0.3071.115) (驱动程序信息:chromedriver=2.30.477691(6ee44a7247c639c0703f291d320bdf05c1531b57),平台=Linux 4.4.0-83-generic x86_64)

为了您的方便,我附上了png文件。 这是网站中的页面源

<input name="ctl00$MasterHeaderPlaceHolder$ctl00$userNameTextbox" type="text" value="Username" id="ctl00_MasterHeaderPlaceHolder_ctl00_userNameTextbox" OnClick="UsernameBehaviour(&quot;click&quot;, &quot;ctl00_MasterHeaderPlaceHolder_ctl00_userNameTextbox&quot;, &quot;Username&quot;);" OnFocus="UsernameBehaviour(&quot;focus&quot;, &quot;ctl00_MasterHeaderPlaceHolder_ctl00_userNameTextbox&quot;, &quot;Username&quot;);" OnBlur="UsernameBehaviour(&quot;blur&quot;, &quot;ctl00_MasterHeaderPlaceHolder_ctl00_userNameTextbox&quot;, &quot;Username&quot;);" />            
<input name="ctl00$MasterHeaderPlaceHolder$ctl00$passwordTextbox" type="password" id="ctl00_MasterHeaderPlaceHolder_ctl00_passwordTextbox" OnBlur="PasswordBehaviour(&quot;ctl00_MasterHeaderPlaceHolder_ctl00_passwordTextbox&quot;, &quot;ctl00_MasterHeaderPlaceHolder_ctl00_tempPasswordTextbox&quot;, 1);" style="display:none" />
<input name="ctl00$MasterHeaderPlaceHolder$ctl00$tempPasswordTextbox" type="text" value="Password" id="ctl00_MasterHeaderPlaceHolder_ctl00_tempPasswordTextbox" OnClick="PasswordBehaviour(&quot;ctl00_MasterHeaderPlaceHolder_ctl00_passwordTextbox&quot;, &quot;ctl00_MasterHeaderPlaceHolder_ctl00_tempPasswordTextbox&quot;, 2);" OnFocus="PasswordBehaviour(&quot;ctl00_MasterHeaderPlaceHolder_ctl00_passwordTextbox&quot;, &quot;ctl00_MasterHeaderPlaceHolder_ctl00_tempPasswordTextbox&quot;, 2);" />

为了方便起见,我附上了png文件。

如您所见,有两个与输入框相关的密码。当我单击密码输入标记时,javascript似乎在做一些事情。但我不确定

我想知道原因和解决办法

谢谢你抽出时间


请帮助我。

您错误地使用了CSS选择器。它需要有一个绑定到它的元素,但您正在尝试使用属性值进行查找

为了让它工作,您需要将CSS选择器更改为此

解决方案1。

driver.find_element_by_css_selector("input[id='ctl00_MasterHeaderPlaceHolder_ctl00_tempPasswordTextbox']")
解决方案2

由于元素具有名称属性,因此可以使用“按名称查找元素”


嗨,麦迪。谢谢你的回复。很抱歉,用户名输入框工作正常。但是由于密码输入框的原因发生了错误。我想知道这件事。@YuiryKozlenko嗯。那么可能是密码字段“隐藏”的问题吗。style=“显示:无”。这可能会导致问题。将其切换到正确的输入字段。pwd=driver.find_element_by_css_selector(“ctl00$MasterHeaderPlaceHolder$ctl00$tempPasswordTextbox”)@YuiryKozlenko当前您正试图将密钥发送到隐藏密码字段,但为什么不将其发送到显示的字段?驱动程序。通过css选择器(“ctl00$MasterHeaderPlaceHolder$ctl00$tempPasswordTextbox”)查找元素嗯,让我看看。我认为问题可能是Selenium没有很好地处理下面的符号$,这就产生了例外。你可以试试驱动程序。通过css选择器(“输入[id='ctl00\U MasterHeaderPlaceHolder\U ctl00\U tempPasswordTextbox'])查找元素。让我们来看看。
driver.find_element_by_name("ctl00$MasterHeaderPlaceHolder$ctl00$tempPasswordTextbox")