How to select 'username' field on website

2019-07-28 05:35发布

All I'm trying to do is enter a username and password...

I tried:

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

but it gave me the error:

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

I just copy and pasted the source for the form below.

<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条回答
叼着烟拽天下
2楼-- · 2019-07-28 05:54

If you want to select by css selector, you'll have to use the correct css selector. You'll want something like this:

username=driver.find_element_by_css_selector('input[name="UserId"]')
查看更多
Viruses.
3楼-- · 2019-07-28 05:58

The method find_element requires 2 arguments:

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

You could also use find_element_by_name

username=driver.find_element_by_name("UserId")
username.send_keys("test")
查看更多
登录 后发表回答