我在选择puppeteer CSS选择器时遇到问题

时间:2020-01-30 08:00:39

标签: javascript html css puppeteer

我需要打字机页面。键入css select。

<div class="preloader">
  <div class="cssload-speeding-wheel"></div>
</div>
<section id="wrapper" class="login-register">
  <div class="login-box">
      <div class="white-box">
      <form class="form-horizontal form-material" id="loginform" method="post">
        <h3 class="box-title m-b-20">Sign In</h3>
        <div class="form-group ">
          <div class="col-xs-12">
            <input class="form-control" type="text" required="" name="login-username" placeholder="Username">
          </div>
        </div>
        <div class="form-group">
          <div class="col-xs-12">
            <input class="form-control" type="password" required="" name="login-password"  placeholder="Password">
          </div>
        </div>
         <div class="form-group">
          <div class="col-xs-12">
        <div class="form-group">
          <div class="col-md-12">

        </div>
        <div class="form-group text-center m-t-20">
          <div class="col-xs-12">
            <button type="submit" name="doLogin" class="btn btn-info btn-lg btn-block text-uppercase waves-effect waves-light" type="submit">Log In</button>
          </div>
        </div>
        <div class="form-group m-b-0">

我需要用户名,密码和登录名。

page.type(“ input [@ placeholder ='Username']”,'MYUSERNAME');

此代码不起作用。请帮助我

2 个答案:

答案 0 :(得分:0)

选择器中不需要@,并且占位符是伪元素。您可以为选择器使用另一个属性:

page.type("input[name='login-username']", 'MYUSERNAME');

尽管我建议您为您的元素提供ID,因为这样可以提高选择器的性能:

<input id="username" class="form-control" type="text" required="" name="login-username" placeholder="Username">

然后

page.type('#username', 'MYUSERNAME');

答案 1 :(得分:0)

我认为这对您解决问题会有所帮助。我试图使其更易于理解。这样您就可以阐明您的概念。

  /**
 * seperate your field information
 * 
 */
let loginAccount ={
    username:"input[name='login-username']",
    password:"input[name='login-password']",
    loginSubmit:".btn"
}
 /**  Enter username */
 await page.waitForSelector(loginAccount.username);
 await page.click(loginAccount.username);
 await page.keyboard.type('krdheeraj51');
 /** Similiarly for login password */
 await page.waitForSelector(loginAccount.password);
 await page.click(loginAccount.password);
 await page.keyboard.type('enter your password');
 /** click your submit button */
 await page.waitFor(2000);
 await page.waitForSelector(loginAccount.loginSubmit);
 await page.click(loginAccount.loginSubmit);