我正在尝试关闭此页面上的框架。
我想要的是点击这里:
这似乎很简单,但到目前为止以下代码(应该可以工作)失败了:
import selenium.webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = selenium.webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.bvc.com.co/variable-income-local-market/cemargos?tab=issuer-information')
#X(close) bvc frame
xpath = '//*[@id="__next"]/div/div[1]/div/div[1]/div/div/div/div/div[3]/div/div/div/div[3]/div[2]/span'
class_name = 'sc-843139d2-14 iVPGqd'
# Trying with XPath
if 1:
try:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, xpath))).click()
except:
driver.find_element(By.XPATH, xpath).click()
# Trying with class_name
if 1:
try:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, class_name))).click()
except:
driver.find_element(By.CLASS_NAME, class_name).click()
使用 XPath 的输出:
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
#0 0x64a95375031a <unknown>
...
使用class_name的输出:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".sc-843139d2-14 iVPGqd"}
关闭该弹出窗口的一个简单方法是单击“X”。您可以使用下面的 XPath 来做到这一点,
//span[text()='X']