【问题标题】:Unable to run headless selenium on heroku with javascript enabled在启用了 javascript 的情况下,无法在 heroku 上运行无头硒
【发布时间】:2020-04-13 07:03:25
【问题描述】:

我正在尝试使用在 Heroku 上的 Spring Boot 应用程序中运行的无头硒来抓取一些 twitter 数据。我的 Heroku 应用程序中包含以下 2 个构建包:

1) https://github.com/heroku/heroku-buildpack-chromedriver.git

2) https://github.com/heroku/heroku-buildpack-google-chrome.git

这两个构建包专为在 heroku 应用中运行无头硒而设计。 在 selenium 脚本输入我的 twitter 登录数据并点击登录按钮后,我的脚本不断崩溃。为什么?我检查了页面源,从 twitter 中发现了以下提示:

我们检测到您的浏览器禁用了 JavaScript。你会 想继续使用旧版 Twitter?

很明显,javascript 似乎被禁用了,这非常糟糕。而且我不想继续使用旧模式(没有 javascript)。

在初始化 webdriver 时,我尝试了以下两个选项来确保启用了 javascript。但似乎被忽略了:

选项 1)

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless", "--enable-javascript");
WebDriver webdriver =  ChromeDriver(chromeOptions);

选项2)

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setJavascriptEnabled(true);
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
WebDriver webdriver =  ChromeDriver(capabilities);

有什么想法吗?

【问题讨论】:

    标签: java google-chrome selenium-webdriver heroku google-chrome-headless


    【解决方案1】:

    我遇到了类似的问题。我通过删除一些选项来解决它。

    这是我目前的驱动程序选项(python):

    options = Options()
    options.binary_location = os.getenv("GOOGLE_CHROME_BIN")
    options.add_argument("--headless")
    options.add_argument("--disable-gpu")
    options.add_argument("--no-sandbox")
    options.add_argument("--window-size=1920x1080")
    driver = webdriver.Chrome(options=options, executable_path=os.getenv("CHROMEDRIVER_PATH"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      • 2021-08-31
      相关资源
      最近更新 更多