Skip to main content
 首页 » 编程设计

python之如何通过 Selenium 和 Python 根据 html 找到元素的 Xpath

2025年05月04日10exmyth

在 python 中尝试过类似的东西,我无法使用 Selenium 单击交叉。

driver.find_element_by_xpath("//span[contains(@onclick, 'parent.$WZRK_WR.closeIframe('60005','intentPreview');')]").click() 

请您参考如下方法:

根据您共享的 HTML 以单击描述为 X 的元素,首先您需要在切换到<iframe>并再次诱导 WebDriverWait 使所需的元素可点击,您可以使用以下解决方案:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='wiz-iframe-intent']"))) 
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='CT_Interstitial']//span[@class='CT_InterstitialClose']"))).click() 

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support import expected_conditions as EC