Linux 使用 javascript 的山猫

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9353519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:40:14  来源:igfitidea点击:

Lynx with javascript

linuxbrowser-automationlynx

提问by TheNotSoWise

So I wrote a script in PHP that requires javascript to run with it. I've had a hard time finding the solution on how to run the javascript with php at the same time using some linux tool to make it automatic (since running it in my browser isn't a choice, I need it to be a crontab). Using the "php" command isn't a choice either since it does not run javascript. So I found a solution, it was lynx.

所以我用 PHP 编写了一个脚本,需要 javascript 来运行它。我很难找到有关如何同时使用 php 运行 javascript 的解决方案,使用一些 linux 工具使其自动(因为在我的浏览器中运行它不是一个选择,我需要它是一个 crontab )。使用“php”命令也不是一种选择,因为它不运行 javascript。所以我找到了一个解决方案,它是lynx。

So here is my problem: for some reason, the javascript runs in my browser just fine, but not in lynx. Using lynx the page just loads without any javascript involvement. Is there a way to debug javascript in lynx, kind of what google chrome has where you can view what's going on from console, so I can find out what's causing the script not to run in lynx?

所以这是我的问题:出于某种原因,javascript 在我的浏览器中运行得很好,但不在 lynx 中。使用 lynx 页面只需加载,无需任何 javascript 参与。有没有办法在 lynx 中调试 javascript,就像 google chrome 那样,您可以从控制台查看正在发生的事情,这样我就可以找出导致脚本无法在 lynx 中运行的原因?

Edit:

编辑:

So apparently lynx does not support javascript. In that case, my question is, what does? I need to run this script as a browser would but from the linux command prompt. And if OS matters in this case, I am running latest Ubuntu.

所以显然lynx不支持javascript。在那种情况下,我的问题是,什么是?我需要像浏览器一样运行此脚本,但需要从 linux 命令提示符运行。如果在这种情况下操作系统很重要,我正在运行最新的 Ubuntu。

采纳答案by sh-beta

Lynx doesn't support Javascript.

Lynx 不支持 Javascript。

Update 1

更新 1

Based on your summary of what you're trying to do (login to a site that requires JavaScript for the password encryption) I'd strongly recommend you look at using Seleniumor another browser automation package. Even if you get the password submission working properly without such a layer, the site's probably going to have other issues if you use a minimalist browser like Lynx.

根据您对尝试执行的操作的总结(登录需要使用 JavaScript 进行密码加密的站点),我强烈建议您考虑使用Selenium或其他浏览器自动化包。即使您在没有这样的层的情况下使密码提交正常工作,如果您使用像 Lynx 这样的极简浏览器,该站点也可能会遇到其他问题。

回答by Thomas

Lynx does not support JavaScript, but the alternative text browser ELinksdoes, based on SpiderMonkey. It's somewhat experimental though; see the manualon how to compile it in.

Lynx 不支持 JavaScript,但基于 SpiderMonkey的替代文本浏览器ELinks支持。不过,这有点实验性;请参阅有关如何编译它的手册

Also, I'm really puzzled about what you're trying to accomplish here...

另外,我真的很困惑你要在这里完成什么......

回答by Jonathan

Selenium is the way to go, but as you notice, it needs a display. Which then needs to be launched, and yada yada yada, now you have quite a complex setup going on!!

Selenium 是可行的方法,但正如您所注意到的,它需要一个显示器。然后需要启动,yada yada yada,现在你有一个相当复杂的设置!

Thankfully, there's yet anotherway to automate all that, and it's through an automation framework. I recommend Jenkins. It already has an addon to launch an x server.

值得庆幸的是,还有另一种方法可以自动化所有这些,它是通过自动化框架。我推荐詹金斯。它已经有一个插件来启动一个 x 服务器。

So the approximate pipeline is as follows:

所以大致的流水线如下:

  • Forget about crontab
  • Setup a jenkins ubuntu server (with x installed)
  • Install the jenkins xvncserver(?) addon -- I think that's what it's called. Anyway this will launch an XHOST for you automatically.
  • Write selenium webdriver scripts that go to the location in question (easiest * is to use the Selenium IDE)
  • Have the selenium IDE output the webdriver scripts to any format, then have jenkins run it. I personally use the Java format, then use ant to build them.
  • 忘记 crontab
  • 设置 jenkins ubuntu 服务器(安装了 x)
  • 安装 jenkins xvncserver(?) 插件——我认为这就是它的名字。无论如何,这将自动为您启动 XHOST。
  • 编写转到相关位置的 selenium webdriver 脚本(最简单的 * 是使用 Selenium IDE)
  • 让 selenium IDE 将 webdriver 脚本输出为任何格式,然后让 jenkins 运行它。我个人使用Java格式,然后使用ant来构建它们。

As you can imagine, this is quite a setup, but it technically will do what you are asking. I'm sorry it's not as simple as piping wget to lynx.

正如您可以想象的那样,这是一个相当大的设置,但从技术上讲,它可以满足您的要求。对不起,它不像将 wget 管道传输到 lynx 那样简单。

Another way to go is just rewrite your webpage to DO STUFF without assuming a browser, for example, take a GET parameter that puts it in "auto mode" and then it will assume it's run from a cron and do it's happiness through a shorter circuit.

另一种方法是在不假设浏览器的情况下将您的网页重写为 DO STUFF,例如,采用 GET 参数将其置于“自动模式”,然后它会假设它是从 cron 运行的,并通过更短的电路来实现它的快乐.

回答by Alexander Skiller

What you need is called a Headless Browser. For example PhantomJSis one of them. I think it is the most popular one.

您需要的是无头浏览器。例如PhantomJS就是其中之一。我认为它是最受欢迎的一种。

回答by JepZ

If you only want to execute Javascriptfrom the command line I would advise you something like nodejsor otto. The have a much smaller performance footprint as a whole headless browser.

如果您只想从命令行执行 Javascript,我会建议您使用nodejsotto 之类的东西。作为整个无头浏览器,它们的性能占用要小得多。

If you need more than just plain Javascript, e.g. the DOM or so, you should try a headless browser(Chrome, PhantomJS) as suggested by others.

如果您需要的不仅仅是普通的 Javascript,例如 DOM 等,您应该尝试其他人建议的无头浏览器ChromePhantomJS)。

If you not only need a browser, but want to do automated testingwith that browser, Seleniumis still the way to go.

如果您不仅需要浏览器,还想使用该浏览器进行自动化测试Selenium仍然是您的最佳选择。

It really depends on your use case how much you need.

这实际上取决于您的用例需要多少。

Update:Since a few months it is possible to use Chrome as a headless Browsernow (Firefox is on its way too).

更新:几个月后,现在可以将Chrome 用作无头浏览器(Firefox 也正在开发中)。