【问题标题】:Selenium – Headless Chrome invalid syntaxSelenium – Headless Chrome 无效语法
【发布时间】:2018-07-15 12:02:21
【问题描述】:

我正在尝试执行无头 Chrome 文本抓取,但遇到了问题。每当我运行下面的代码时,我都会收到一条错误消息:

ChromeOptions 选项 = new ChromeOptions(); SyntaxError: 无效的语法。

关于如何解决这个问题的任何想法?非常感谢!

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

ChromeOptions options = new ChromeOptions();
options.addArguments("window-size=1400,800");
options.addArguments("headless");

WebDriver driver = new ChromeDriver(options);

driver.get('https://www.samplehomepage.com')

latest_comment = driver.find_element_by_css_selector('p')
print(latest_comment.text)

【问题讨论】:

  • 我看到一些带有一些 Java 或 C# 的 python。你不能在同一个脚本中使用两种语言。

标签: selenium headless


【解决方案1】:

如果您使用的是 java ,您可以使用以下代码在无头 chrome 浏览器上工作。

System.setProperty("webdriver.chrome.driver", "path to chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
options.addArguments("window-size=1200x600");
WebDriver driver = new ChromeDriver(options);
driver.get("https://google.com");

【讨论】:

    猜你喜欢
    • 2021-12-27
    • 2020-04-16
    • 2019-05-25
    • 2018-02-14
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    相关资源
    最近更新 更多