这怪不得ai
因为ai没法帮你去测试代码,所以它并不知道查询结果会打开一个新的窗口,你的代码因为没有切换窗口所以找不到查询结果的元素
以下是我帮你修改后的代码:import time
from selenium import webdriver
from selenium.webdriver.common.by import By
option = webdriver.EdgeOptions()
option.add_experimental_option("detach", True)
option.add_experimental_option("excludeSwitches", ["enable-automation"])
option.add_experimental_option("useAutomationExtension",False)
option.add_argument("User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0")
browser = webdriver.Edge(options=option)
browser.get("https://eproport.crecgec.com/#/home")
time.sleep(2)
browser.find_element(By.XPATH,"/html/body/app-root/layout-fullscreen/div[2]/div/div/nz-input-group/span/input").send_keys("钢")
time.sleep(3)
browser.find_element(By.XPATH,"/html/body/app-root/layout-fullscreen/div[2]/div/div/nz-input-group/span/span/button/span[2]").click()
time.sleep(3)
# 添加的代码 **************************************************************
# 获取当前窗口句柄
current_window = browser.current_window_handle
# 获取所有窗口句柄
all_windows = browser.window_handles
# 切换到新打开的窗口
for window in all_windows:
if window != current_window:
browser.switch_to.window(window)
break
# **********************************************************************************
browser.find_element(By.XPATH,"/html/body/app-root/layout-fullscreen/div[5]/app-nobanner/app-notice-notice/nz-card[2]/div/nz-table/nz-spin/div/div/nz-table-inner-default/div/table/tbody/tr[1]/td[1]/span/a").click()
time.sleep(3)
|