-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathexample_basic.py
More file actions
49 lines (36 loc) · 1.64 KB
/
example_basic.py
File metadata and controls
49 lines (36 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from testcontainers.selenium import SeleniumContainer
def basic_example():
with SeleniumContainer() as selenium:
# Get the Selenium WebDriver
driver = selenium.get_driver()
try:
# Navigate to a test page
driver.get("https://www.python.org")
print("Navigated to python.org")
# Wait for the search box to be present
search_box = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "id-search-field")))
# Type in the search box
search_box.send_keys("selenium")
print("Entered search term")
# Click the search button
search_button = driver.find_element(By.ID, "submit")
search_button.click()
print("Clicked search button")
# Wait for search results
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "list-recent-events")))
# Get search results
results = driver.find_elements(By.CSS_SELECTOR, ".list-recent-events li")
print("\nSearch results:")
for result in results[:3]: # Print first 3 results
print(result.text)
# Take a screenshot
driver.save_screenshot("python_search_results.png")
print("\nScreenshot saved as 'python_search_results.png'")
finally:
# Clean up
driver.quit()
if __name__ == "__main__":
basic_example()