Hatua ya 1: Sakinisha Selenium
Fungua terminal au command prompt na uendesha amri ifuatayo ili kusakinisha Selenium maktaba kupitia bomba:
pip3 install selenium
Hatua ya 2: Pakua na Sakinisha WebDriver
Sawa na njia iliyoelezwa katika majibu ya awali, unahitaji kupakua na kusakinisha WebDriver sambamba na kivinjari unachotaka kutumia.
Hatua ya 3: Andika Python Msimbo
Ufuatao ni mfano wa jinsi ya kutumia Selenium kufungua ukurasa wa wavuti, kufanya utafutaji, na kurejesha maudhui:
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()
Kumbuka kuwa mfano hapo juu unatumia kivinjari cha Chrome. Ikiwa unataka kutumia kivinjari tofauti, unahitaji kubadilisha webdriver.Chrome()
na webdriver.Firefox()
au webdriver.Edge()
kulingana na kivinjari unachotaka kutumia.
Kumbuka Muhimu
- Selenium inahitaji a WebDriver kudhibiti kivinjari. Hakikisha umesakinisha na kusanidi njia sahihi ya WebDriver.
- Unapotumia kufanyia kazi Selenium mwingiliano wa kivinjari kiotomatiki, kumbuka kuingiliana na hatua za usalama kwenye tovuti na uzingatie sera za tovuti.