如何使用 Selenium 导航到侧面菜单链接

问题描述 投票:0回答:1

我正在努力使用 Selenium 单击弹出窗口的左侧菜单。我想从主弹出窗口导航到左侧菜单,单击名为“Unaffirmed”的标题链接。我查看了弹出窗口的 HTML 代码,这部分是导航窗口所在的位置。不太确定如何解决这个问题。

在此输入图片描述

我想做的就是导航到左手菜单并单击其中名为“未确认”的链接。

python selenium-webdriver
1个回答
0
投票
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time

# Initialize the driver
driver = webdriver.Chrome()

# Navigate to the main page that opens the pop-up
driver.get('URL_of_the_main_page')

# Code to open the pop-up window
# Assuming a button click or some interaction opens the pop-up
driver.find_element(By.ID, 'button_to_open_popup').click()

# Wait for the pop-up to open
time.sleep(5)  # Adjust as necessary

# Get the handle of the current window
main_window_handle = driver.current_window_handle

# Get all window handles
all_window_handles = driver.window_handles

# Find the new window handle
for handle in all_window_handles:
    if handle != main_window_handle:
        popup_window_handle = handle
        break

# Switch to the pop-up window
driver.switch_to.window(popup_window_handle)

# If the menu is inside an iframe, switch to the iframe
# Assuming the iframe has a name or ID, otherwise find it by index
driver.switch_to.frame('iframe_name_or_id')

# Locate the "Unaffirmed" link and click it
unaffirmed_link = driver.find_element(By.LINK_TEXT, 'Unaffirmed')
unaffirmed_link.click()

# If needed, switch back to the main window
driver.switch_to.window(main_window_handle)

# Close the driver
driver.quit()
   
© www.soinside.com 2019 - 2024. All rights reserved.