【发布时间】:2017-03-01 03:09:35
【问题描述】:
以下是网站的 HTML:
<td class="loginPassword">
<input name="_ctl0$MainContentPlaceHolder$oSession$oPwdID" type="password" maxlength="50" id="oPwdID" tabindex="2" class="signin login-input-rounded" autocomplete="off" data-i18n="[title]LoginPage.Password" title="Password" style="display: none;">
<input name="_ctl0$MainContentPlaceHolder$oSession$txtPlainPassword" type="text" id="txtPlainPassword" tabindex="2" class="graytext login-input-rounded" data-i18n="[value]LoginPage.Password" value="Password" style="display: inline-block;">
<input type="hidden" name="_ctl0$MainContentPlaceHolder$oSession$elqCustomerGUID" id="elqCustomerGUID"></td>
这是我的代码:
findPasswordField = driver.find_element_by_id("txtPlainPassword").click()
driver.execute_script("document.querySelector('#txtPlainPassword').style.display='block';")
driver.find_element_by_id("txtPlainPassword").send_keys("papsp")
它点击了密码元素,但没有输入字符串“papsp”。
它会抛出以下错误消息:
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element
我尝试使用以下 stackoverflow 答案,这里是 a link!
我不确定我做错了什么,我认为它应该可以工作。
如果有任何不清楚或需要更多信息,请告诉我。
【问题讨论】:
-
一般来说,您不希望使用 JSE 来更改元素的状态,因为用户不会以这种方式与页面交互。找出元素如何启用、可见等并对其进行编码。如果您要分享该页面的链接,我们可以看看。
-
为什么不用JS直接设置值呢?
标签: python selenium selenium-webdriver