PhantomJS-通过jQuery登录到Twitch [英] PhantomJS - Login to Twitch Through jQuery
问题描述
我正在寻找一种通过PhantomJS抽搐登录的方法.当我浏览"e.png"时,我现在拥有的代码无法正常工作
I am looking for a way to login to twitch through PhantomJS. The code I have now is not working when I look through "e.png"
var page = require('webpage').create();
page.open('http://www.twitch.tv/login', function() {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
page.evaluate(function() {
$("login_user_login").val("username");
$("user[password]").val("password");
$("button").click(); // click login button
page.render("e.png"); // see if anything happens
});
phantom.exit();
});
});
我在这里找到了有关如何通过硒进行操作的链接: 硒抽搐
I have found a link on how to do it through selenium here: Selenium Twitch
HTML(针对选择器,根据请求): https://jsfiddle.net/r8szj7wy/1/
HTML (For selectors, per request): https://jsfiddle.net/r8szj7wy/1/
<input class="field text med-long" id="login_user_login" name="user[login]" tabindex="8" type="text" />
<input class="field text med-long" id="user[password]" name="user[password]" tabindex="9" type="password" value="" />
<button class="button primary" tabindex="11" type="submit">Log In
推荐答案
不可能的渲染
page.evaluate()
是沙盒页面上下文.它无权访问page
或在其外部定义的任何其他变量.您需要将page.render("e.png")
前后移动才能看到任何内容.
Impossible render
page.evaluate()
is the sandboxed page context. It doesn't have access to page
or any other variables defined outside of it. You need to move the page.render("e.png")
outside and behind it to see anything.
您的选择器错误. login_user_login
和user[password]
应该是id,但是您编写CSS选择器时就好像DOM中有login_user_login
和user
元素一样,并非如此.正确的ID选择器应为#login_user_login
和[id='user[password]']
.请注意,第二个必须与第一个不同地编写,因为它包含为属性选择器保留的[]
.
Your selectors are wrong. login_user_login
and user[password]
are supposed to be ids, but you're writing the CSS selector as if there are login_user_login
and user
elements in the DOM which is not the case. Correct ID selectors would be #login_user_login
and [id='user[password]']
. Note that the second one must be written differently than the first one, because it contains []
which is reserved for attribute selectors.
最后,单击登录后需要一段时间,因此您需要等待页面重新加载.您可以使用setTimeout
使用静态超时,或者使用 <例子中的c11> .
And finally, after you click to login, it takes a while, so you need to wait until the page has reloaded. You can use a static timeout using setTimeout
or a more sophisticated method using waitFor
from the examples.
最终的简单脚本:
var page = require('webpage').create();
page.open('http://www.twitch.tv/login', function() {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
page.evaluate(function() {
$("#login_user_login").val("username");
$("[id='user[password]']").val("password");
$(".button.primary:first").click(); // click login button
});
setTimeout(function(){
page.render("e.png"); // see if anything happens
phantom.exit();
}, 5000); // 5 seconds
});
});
这篇关于PhantomJS-通过jQuery登录到Twitch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!