【发布时间】:2021-07-05 02:36:47
【问题描述】:
在这个平台上也问过这个问题:https://www.reddit.com/r/selenium/comments/mig603/moved_to_linux_cant_get_my_web_scraper_to_work/
我以前在 Windows 上,换到 Linux Pop!_OS 20.10。由于我换了,我不能让我的 WebScraper 工作了,这个问题看起来很简单,但我无法解决它。
代码:
File pathBinary = new File("/lib/firefox/");
FirefoxBinary firefoxBinary = new FirefoxBinary(pathBinary);
System.setProperty("webdriver.gecko.driver", "/geckodriver_linux");
// System.setProperty("webdriver.chrome.driver", "/home/benij/Software/chromedriver_linux64/chromedriver");
// Removing unnecessary JavaScript errors/warnings.
System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE, "true");
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "/dev/null");
// FirefoxProfile profile = new FirefoxProfile("");
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("dom.webdriver.enabled", false);
profile.setPreference("useAutomationExtension", false);
profile.setPreference("permissions.default.image", 2);
profile.setPreference("dom.ipc.plugins.enabled.libflashplayer.so", false);
profile.setPreference("webgl.disabled", true);
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
options.setBinary(firefoxBinary);
// options.addArguments("--headless");
// WebDriver webDriver = new ChromeDriver();
WebDriver webDriver = new FirefoxDriver(options);
webDriver.manage().window().maximize();
// Sets max loading time for a website.
webDriver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
例外:
Exception in thread "main" java.lang.IllegalStateException: Specified firefox binary location does not exist or is not a real file: /lib/firefox
at com.google.common.base.Preconditions.checkState(Preconditions.java:504)
at org.openqa.selenium.firefox.Executable.<init>(Executable.java:43)
at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:123)
at Main.main(Main.java:24)
文件结构:
benij@benij:/lib/firefox$ ls
application.ini gmp-clearkey libmozwayland.so libssl3.so
browser gtk2 libnspr4.so libxul.so
crashreporter libfreeblpriv3.chk libnss3.so minidump-analyzer
crashreporter.ini libfreeblpriv3.so libnssckbi.so omni.ja
defaults liblgpllibs.so libnssutil3.so platform.ini
dependentlibs.list libmozavcodec.so libplc4.so plugin-container
distribution libmozavutil.so libplds4.so Throbber-small.gif
firefox libmozgtk.so libsmime3.so
firefox.sh libmozsandbox.so libsoftokn3.chk
fonts libmozsqlite3.so libsoftokn3.so
此外,当在终端中运行“firefox”时,firefox 已启动,因此通常我不必告诉我的操作系统 firefox 在哪里,因为它在路径中。但是在删除 FirefoxBinary 时会发生这种情况:
没有 FirefoxBinary 的异常:
Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: LINUX
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'benij', ip: '192.168.1.108', os.name: 'Linux', os.arch: 'amd64', os.version: '5.11.0-7612-generic', java.version: '15.0.2'
Driver info: driver.version: FirefoxDriver
at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:100)
at java.base/java.util.Optional.orElseGet(Optional.java:362)
at org.openqa.selenium.firefox.FirefoxOptions.getBinary(FirefoxOptions.java:216)
at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:187)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:147)
at Main.main(Main.java:46)
【问题讨论】:
标签: java linux selenium firefox geckodriver