-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_selenium.py
48 lines (37 loc) · 1.47 KB
/
test_selenium.py
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
import traceback
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
def main():
print("Starting Selenium test...")
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
try:
print("Initializing webdriver...")
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)
print("Navigating to example.com...")
driver.get("https://www.example.com")
print("Page title:", driver.title)
print("Waiting for h1 element...")
wait = WebDriverWait(driver, 10)
h1 = wait.until(EC.visibility_of_element_located((By.TAG_NAME, "h1")))
print("H1 text:", h1.text)
print("Taking screenshot...")
driver.save_screenshot("screenshot.png")
print("Screenshot saved as screenshot.png")
except Exception as e:
print(f"An error occurred: {str(e)}")
print("Traceback:")
traceback.print_exc()
finally:
if "driver" in locals():
driver.quit()
print("Test completed.")
if __name__ == "__main__":
main()