我正在使用 Selenium、Python 抓取页面。打开页面时会出现一个弹出窗口。无论如何,我想关闭此弹出窗口。我试过如下:
url = https://shopping.rochebros.com/shop/categories/37
browser = webdriver.Chrome(executable_path=chromedriver, options=options)
browser.get(url)
browser.find_element_by_xpath("//button[@class='click' and @id='shopping-selector-parent-process-modal-close-click']").click()
我在这里尝试了几个类似的帖子,但没有任何效果。在错误之下,我得到了。
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class='click' and @id='shopping-selector-parent-process-modal-close-click']"}
所需元素是模态对话框中的一个<button>
标签,因此要单击所需元素,您需要诱导WebDriverWait以使该元素可点击,您可以使用以下解决方案:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe')
driver.get("https://shopping.rochebros.com/shop/categories/37")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='close' and @id='shopping-selector-parent-process-modal-close-click']"))).click()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句