Tabbas, bari mu fara da gabatar da batun buɗe hanyar haɗi a cikin Java. Kewaya yanar gizo ko hulɗa tare da URLs wani muhimmin sashi ne na shirye-shirye ta hanyoyi da yawa. Tsarin buɗe hanyar haɗin yanar gizo a cikin Java ya ƙunshi amfani da ko dai Desktop ko Laburaren Browser, ya danganta da buƙatunku.
Laburare Desktop wani yanki ne na daidaitattun ɗakunan karatu na Java kuma yana ƙunshe da hanyoyin aiwatar da ayyuka kamar buɗe URL a cikin tsoho mai bincike.
import java.awt.Desktop; import java.net.URI; public class Main { public static void main(String[] args) { if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { try { Desktop.getDesktop().browse(new URI("http://example.com")); } catch (Exception e) { e.printStackTrace(); } } } }
Wannan samfurin samfurin yana bincika idan Desktop yana da tallafi akan tsarin kuma yana buɗe ƙayyadadden URL a cikin tsoho mai bincike.
Gabatarwar Laburare Mai Sauri
The Laburaren Browser wani zaɓi ne na ɓangare na uku wanda ke ba da ƙarin cikakken iko akan tsarin bincike. Yana goyan bayan dandamali daban-daban da fasali da yawa, kamar saita mai binciken da za a yi amfani da shi ko wakilin mai amfani. Shahararren misali na irin waɗannan ɗakunan karatu shine Selenium WebDriver.
[h2]Labarun Bincike a Java – Selenium WebDriver
Selenium WebDriver shine tushen tushen tushen tsarin da ake amfani da shi galibi don sarrafa aikace-aikacen yanar gizo don dalilai na gwaji. Yana goyan bayan yarukan shirye-shirye da yawa da masu bincike don sarrafa ayyukan da galibi za ku yi da hannu akan shafin yanar gizon.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Main { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "path_to_geckodriver"); WebDriver driver = new FirefoxDriver(); driver.get("http://example.com"); } }
A cikin wannan misalin lambar Java, muna amfani da Selenium WebDriver tare da mai binciken Firefox. Layin 'System.setProperty…' yana saita wurin takamaiman direban mai bincike, wanda a cikin yanayinmu shine "geckodriver" don Firefox. Ana amfani da abun WebDriver don buɗe URL ɗin.