Кодировка показана ниже:
Код: Выделить всё
import time
import pygetwindow as gw
import pyautogui
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import TimeoutException
def dropdown_labels(driver, ID):
"""
Click the dropdown to open it, then take a screenshot using PyAutoGUI after it is expanded.
"""
try:
# Wait for the dropdown element to be clickable, then click it to expand
dropdown = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, ID)))
# Create an instance of ActionChains
actions = ActionChains(driver)
# Click and hold on the dropdown without releasing
actions.click_and_hold(dropdown).perform()
# Wait for the dropdown to become visible
dropdown_options = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, f"//select[@id='{ID}']/option"))
)
# Capture the screenshot after the dropdown is opened
time.sleep(2) # Allow time for the dropdown to visually expand
screenshot_name = f"dropdown_screenshot_{ID}.png"
driver.save_screenshot(screenshot_name)
print(f"Screenshot saved as {screenshot_name}")
except TimeoutException:
print(f"Dropdown with ID '{ID}' not found or clickable.")
return None
def test_test():
# Initialize WebDriver
driver = webdriver.Chrome()
# Open the target webpage
driver.get("http://192.168.77.81/auth") # Replace with your actual URL
driver.set_window_size(1471, 975)
driver.execute_script("window.focus();")
# Perform actions to navigate to the dropdown
driver.find_element(By.CSS_SELECTOR, ".language_container:nth-child(4)").click()
driver.find_element(By.LINK_TEXT, "CONFIG RX").click()
# Capture screenshot of the dropdown for "baud"
dropdown_labels(driver, "baud")
# Quit the driver after task is complete
driver.quit()
if __name__ == "__main__":
test_test()
- Нажмите и удерживайте с помощью цепочек действий
- Временные задержки с использованием time.sleep на несколько секунд
- asyncio с исключениями типа try и исключая
- выполнение javascript в раскрывающемся списке попробуйте отобразить его отдельно
- Опция Google Chrome без заголовка в Selenium
Подробнее здесь: https://stackoverflow.com/questions/790 ... enu-in-pyt