javascript - 无法使用 python selenium 登录 namecheap

标签 javascript python selenium selenium-webdriver

您好,我正在尝试使用 selenium 登录 namecheap.com,但无法选择登录表单。选择元素时出现此错误:

selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with

但我可以看到我选择了确定的 xpath。 这是我的代码:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common import action_chains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time

cookie_file_path = 'cookie.txt'

dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] =("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36")

# driver = webdriver.PhantomJS(desired_capabilities=dcap,service_args=['--ssl-protocol=any','--ignore-ssl-errors=true','--cookies-file={}'.format(cookie_file_path)])
driver=webdriver.Firefox()
driver.set_window_size(1024, 768)
username='test'
password='test123'

driver.get('https://www.namecheap.com/myaccount/login.aspx')

driver.find_element_by_xpath("//input[@name='LoginUserName']").send_keys(username)
driver.find_element_by_xpath('//input[@name="LoginPassword"]').send_keys(password)
loginButtonXpath = "//input[@type='submit']"
driver.find_element_by_xpath(loginButtonXpath).click()
driver.save_screenshot('testing.png')

这是 HTML:

                    <div class="sign-in-form">
                    <fieldset>
                        <input type="text" name="LoginUserName" title="Username" placeholder="Username" autocapitalize="off" autocorrect="off" class="input removespecialchars handlereturn" maxlength="20" />
                        <input type="password" name="LoginPassword" title="Password" placeholder="Password" class="input handlereturn" maxlength="100" />
                        <input type="hidden" name="hidden_LoginPassword" />
                        <a href="https://www.namecheap.com/myaccount/login.aspx" id="ctl00_ctl00_ctl00_ctl00_base_content_web_base_content_topNavLoginLink" class="head-loginb ncRedirectButton btn sign-in-btn btn-block" rel="nofollow">Sign In</a>
                        <a href="https://ap.www.namecheap.com/ResetPassword" id="ctl00_ctl00_ctl00_ctl00_base_content_web_base_content_passwordRemiderLink" title="Password Reminder">Forgot your password?</a>
                    </fieldset>
                    </div>
                </div>
            </li>
            <li class="hidewhenloggedin"><a href="/myaccount/signup.aspx">Sign Up</a></li>
            <li class="expandable signed-in hidewhennotloggedin  user-menu">

你能帮我一下吗? 谢谢

最佳答案

看来您的 xPath 定位器(用于登录/用户名)是失败的原因。

尝试使用以下 CSS 定位器..

Username
css=div.group > input[name="LoginUserName"]

Password
css=div.group > input[name="LoginPassword"]

通常,与 CSS 定位器相比,xPath 定位器被认为更脆弱。因此,使用 CSS 定位器会很好。

关于javascript - 无法使用 python selenium 登录 namecheap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36193886/

相关文章:

python - 调用 python 脚本时,它是否将完整的脚本加载到终端/命令提示符中?

python - 如何在不混合相应条目的情况下将 2D 矩阵转换为 3D 张量?

java - Selenium StaleElementReferenceException 显式等待

javascript - 如何对齐具有不同列数的两行

javascript - 如何在 react 中合并具有相同键的对象

javascript - 使用 JavaScript 将多维数组保存到 XML 中

javascript - 如何使用 typescript 在node.js中获取具有其他参数的图像文件?

python - 如何使用python电报机器人将pdf文件发送回用户?

python - 如何通过 Robot Framework 或 python 脚本将 csv(逗号分隔)或 xlsx(excel)文件转换为 psv(管道分隔)文件

selenium - 如何从ubuntu容器中获取本地主机URL?