为什么Python库能够下载所需的模块,但Java不能?

huangapple go评论145阅读模式
英文:

Why a python library can download needed modules but java can't?

问题

你的理解是正确的。在Java中,使用像Selenium这样的库时,通常需要手动下载必要的驱动程序可执行文件(例如,ChromeDriver或Firefox的GeckoDriver),并在代码或系统环境中指定其位置。

与Python不同,Python可以通过像pip这样的包管理器轻松安装和管理驱动程序,Java需要手动管理驱动程序可执行文件。

在你的Java代码示例中,你可以看到需要设置系统属性"webdriver.chrome.driver"为下载的ChromeDriver可执行文件的位置。这告诉Selenium库在你的系统上查找驱动程序的位置。

一旦你下载了适当的驱动程序可执行文件并提供了正确的路径,你可以创建驱动程序的实例(例如,ChromeDriver),并在使用它与网页进行交互之前配置任何所需的选项。

尽管与Python相比,这个过程在Java中可能显得更加复杂,但它允许更灵活地管理不同版本的驱动程序,并确保与你的浏览器兼容。

总之,你的理解是正确的。在Java中,需要手动下载和放置驱动程序可执行文件,而Python通过包管理器提供了更加简化的方法来下载和管理驱动程序。

英文:

In some libraries like selenium we can download necessary library or module from code like download Chrome driver or Firefox driver in easy way, but in java code we don't.

We need to download manually and add to the path in project or system environment and after that use it in code providing a path for the library or execute environment.

Java code example:

System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized"); // open Browser in maximized mode
options.addArguments("disable-infobars"); // disabling infobars
options.addArguments("--disable-extensions"); // disabling extensions
options.addArguments("--disable-gpu"); // applicable to windows os only
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("--no-sandbox"); // Bypass OS security model
WebDriver driver = new ChromeDriver(options);
driver.get("https://google.com");

Python code example:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=chrome_options)

as you see the driver can download from python but java need to downloaded and placed somewhere in environment and than use it in code.

Am I wrong or misunderstanding?

chat gpt answer:

>You are correct in your understanding. In Java, when using libraries like Selenium, you typically need to manually download the necessary driver executable (e.g., ChromeDriver or GeckoDriver for Firefox) and specify its location in your code or system environment.
>
>Unlike Python, where you can easily install and manage drivers through package managers like pip, Java requires a manual approach for managing the driver executables.
>
>In your Java code example, you can see that you need to set the system property "webdriver.chrome.driver" to the location of the downloaded ChromeDriver executable. This tells the Selenium library where to find the driver on your system.
>
>Once you have downloaded the appropriate driver executable and provided the correct path, you can create an instance of the driver (e.g., ChromeDriver) and configure any desired options before using it to interact with web pages.
>
>Although the process may seem more involved in Java compared to Python, it allows for greater flexibility in managing different versions of the driver and ensuring compatibility with your browser.
>
>In summary, your understanding is correct. Java requires manual downloading and placement of the driver executable, while Python provides a more streamlined approach for downloading and managing drivers through package managers.

答案1

得分: 1

你的理解是不正确的,ChatGPT的答案也是如此。

使用selenium v4.6.0及更高版本(无论绑定的编程语言是Java、Python、C#等),不再需要设置driver.exe。Selenium的内置工具,称为Selenium Manager,将会以编程方式处理/下载驱动程序。

就像你的Python代码一样,Java代码也可以非常简单:

WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");

下面这行不再是必需的,如果你指定了路径,它将从提供的路径获取driver.exe,否则Selenium会在内部下载并使用它。

System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
英文:

Your understand is incorrect, so is ChatGPT's answer.

With selenium v4.6.0 and onwards (IRRESPECTIVE OF BINDING LANGUAGE, BE IT JAVA, PYTHON, C# etc.), setting up the driver.exe is not mandatory. Selenium's inbuilt tool known as Selenium Manager will handle/download the drivers programmatically.

Just like your Python code, Java code also can be as simple as:

WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");

Below line is not mandatory anymore, if you specify, it will take driver.exe from the provided path, else Selenium will download and use it internally.

System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");

huangapple
  • 本文由 发表于 2023年8月10日 22:13:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定