selenium
Rozpoczęcie pracy z Selenium w Pythonie
Szukaj…
Uwagi
Co to jest selen?
Selenium to biblioteka poleceń, która pomaga interfejsowi programisty z przeglądarką jak prawdziwy użytkownik.
Rzeczy, które robi Selenium:
Znajdowanie elementów w html strony internetowej
Znajduje pojedynczy element:
-
driver.find_element_by_css_selector("css.selector.of.element")
Pomoc do Selektora CSS -
driver.find_element_by_xpath("//xpath//of//element")
Pomoc XPATH -
driver.find_element_by_name("name_of_element")
-
driver.find_element_by_id("id_of_element")
-
driver.find_element_by_partial_link_text("element_link_text")
-
driver.find_element_by_class_name("class_name_of_element")
-
driver.find_element_by_tag_name("tag_name_of_element")
-
Znajduje listę elementów:
-
driver.find_elements_by_css_selector("css.selector.of.elements")
-
driver.find_elements_by_xpath("//xpath//of//elements")
-
driver.find_elements_by_name("name_of_elements")
-
driver.find_elements_by_partial_link_text("elements_link_text")
-
driver.find_elements_by_class_name("class_name_of_elements")
-
driver.find_elements_by_tag_name("tag_name_of_elements")
-
Oficjalna dokumentacja: selenium-python czyta dokumenty
Interakcja z elementami:
„metoda” reprezentuje dowolną z powyższych metod znajdowania elementu lub listy elementów.
funkcja kliknięcia:
-
driver.find_element_by_method.click()
-
funkcja send_keys:
-
driver.find_element_by_method.send_keys("text")
wysyła ciąg „tekst” do znalezionego elementu. -
driver.find_element_by_method.send_keys(KeyCode.UP)
wysyła KeyCode dla klawisza strzałki w górę do znalezionego elementu.
-
Podstawowy pyton Selen
from selenium import webdriver
driver = webdriver.Chrome() # Creates a new chromedriver instance
driver.get("https://www.python.org/") # Go to https://www.python.org/
# Sends the text "python" to the text search box
driver.find_element_by_id("id-search-field").send_keys("python")
# Click on the search button
driver.find_element_by_css_selector("button[type=\"submit\"]").click()
Podstawowa walizka z selenem
Jest to podstawowy przykład testu Selenium przy użyciu biblioteki Unittest w Pythonie
from selenium import webdriver
import unittest
class SeleniumTest(Unittest.testcase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
def test(self):
self.driver.get("https//www.google.com")
self.driver.find_element_by_id("lst-ib").send_keys("python")
self.driver.find_element_by_css_selector("span[class=\"sbico\"]").click()
def tearDown(self):
self.driver.quit()
Modified text is an extract of the original Stack Overflow Documentation
Licencjonowany na podstawie CC BY-SA 3.0
Nie związany z Stack Overflow