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

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

问题描述:

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

I'm trying to log in website with selenium. This is the code that I made.

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 。


raise exception_class(message,screen,stacktrace)
selenium.common.exceptions .InvalidSelectorException:消息:无效选择器:指定了无效或非法选择器
(会话信息:chrome = 59.0.3071.115)
(驱动程序信息:chromedriver = 2.30.477691(6ee44a7247c639c0703f291d320bdf05c1531b57),platform = Linux 4.4 .0-83-generic x86_64)

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified (Session info: chrome=59.0.3071.115) (Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.4.0-83-generic x86_64)

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

这是网站的页面来源。

I've attached png file for your convenience. This is the page source in the website.

<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文件。

I have attached png file for the convenience.

如您所见,有两个输入框相关的密码。当我点击密码输入标签时,javascript似乎做了些什么。但我不确定。

As you can see, there are two input box related password. When I click password input tag, javascript seems to do something. But I'm not sure.

我想知道原因和解决方案。

I would like to know the reason and solution.

谢谢你的支持时间。

请帮助我。

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

You are using CSS selector incorrectly. It needs to have an element tied to it, but you are trying to find using an attribute value.

为了使其正常工作,您需要将CSS选择器更改为此。

In order for it to work you need to change your CSS selector to this.

解决方案1。

driver.find_element_by_css_selector("input[id='ctl00_MasterHeaderPlaceHolder_ctl00_tempPasswordTextbox']")

解决方案2

由于元素具有name属性,因此可以使用find_element_by_name。

As the elements have name attribute, you can use "find_element_by_name".

driver.find_element_by_name("ctl00$MasterHeaderPlaceHolder$ctl00$tempPasswordTextbox")