如何使用Selenium webdriver在网站上选择“用户名”字段?

时间:2016-05-23 15:57:19

标签: python selenium

我要做的就是输入用户名和密码......

我试过了:

username=driver.find_element(By.CSS_SELECTOR("UserId"))
username.send_keys("test")

但它给了我错误:

username=driver.find_element(By.CSS_SELECTOR("UserId"))
TypeError: 'str' object is not callable

我只是复制并粘贴下面表格的来源。

<form name="loginform" id="loginform" method='POST' action="login.jsp" AUTOCOMPLETE="off">

<table class="loginmiddletable" cellspacing=8 cellpadding=2>
    <tr>
        <td style="padding-top:20px" class="loginlabel">Network ID&nbsp;
            <INPUT type="text" class="logininput" value='' name="UserId" >
        </td>
    </tr>
    <tr>
        <td class="loginlabel">Password&nbsp;
            <INPUT type=password class="logininput" value='' name="Password" >
        </td>
    </tr>
    <tr>
        <td align="right" class="errormsgstyle">&nbsp;
            <input class=loginbutton type=submit name="btnLogin" value='Sign In' title="Click To Sign In" onclick="window.status='Signing in. Please wait...';">
        </td>
    </tr>
    <tr>
        <td align="left" class="errormsgstyle">&nbsp;
        </td>
    </tr>
</table>
</form>

2 个答案:

答案 0 :(得分:2)

如果要通过css选择器进行选择,则必须使用正确的css选择器。你会想要这样的东西:

username=driver.find_element_by_css_selector('input[name="UserId"]')

答案 1 :(得分:2)

方法alertController.addTextFieldWithConfigurationHandler({(textField: UITextField!) in textField.placeholder = "Hyperlink" inputTextField = textField inputTextField?.text="www.google.com" }) 需要2个参数:

find_element

您也可以使用username=driver.find_element(By.NAME, "UserId") username.send_keys("test")

find_element_by_name