Langkah 1: Instal Selenium
Bukak terminal utawa command prompt lan jalanake printah ing ngisor iki kanggo nginstal Selenium perpustakaan liwat pip:
pip3 install selenium
Langkah 2: Ngundhuh lan Instal WebDriver
Kaya sing diterangake ing tanggapan sadurunge, sampeyan kudu ngundhuh lan nginstal WebDriver browser sing cocog karo sing pengin digunakake.
Langkah 3: Tulis Python Kode
Ing ngisor iki conto cara nggunakake Selenium kanggo mbukak kaca web, nindakake telusuran, lan njupuk konten:
from selenium import webdriver
# Initialize the browser(using Chrome in this example)
driver = webdriver.Chrome()
# Open a web page
driver.get("https://www.example.com")
# Find an element on the web page
search_box = driver.find_element_by_name("q")
search_box.send_keys("Hello, Selenium!")
search_box.submit()
# Print the web page content after the search
print(driver.page_source)
# Close the browser
driver.quit()
Elinga yen conto ing ndhuwur nggunakake browser Chrome. Yen sampeyan pengin nggunakake browser liyane, sampeyan kudu ngganti webdriver.Chrome()
karo webdriver.Firefox()
utawa webdriver.Edge()
miturut browser sing pengin digunakake.
Wigati Penting
- Selenium mbutuhake WebDriver kanggo ngontrol browser web. Priksa manawa sampeyan wis nginstal lan nyetel dalan sing bener menyang file WebDriver.
- Nalika nggunakake Selenium kanggo ngotomatisasi interaksi browser web, elinga sesambungan karo langkah-langkah keamanan ing situs web lan manut kabijakan situs web kasebut.