在 Selenium Python 中关闭浏览器弹出窗口 [英] Close browser popup in Selenium Python
本文介绍了在 Selenium Python 中关闭浏览器弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Selenium、Python 抓取页面.打开页面时会出现一个弹出窗口.无论如何,我想关闭此弹出窗口.我试过如下:
I am scraping a page using Selenium, Python. On opening the page one Popup appears. I want to close this popup anyway. I tried as below:
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()
我在这里尝试了几个类似的帖子,但没有任何效果.在错误下方,我得到了.
I tried a couple of similar posts here but nothing is working with me. below the error, I am getting.
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class='click' and @id='shopping-selector-parent-process-modal-close-click']"}
推荐答案
所需元素是 Modal Dialog 中的 标签,因此点击您需要为 元素可点击诱导 WebDriverWait 所需的元素,您可以使用以下解决方案:
The desired element is a <button>
tag within a Modal Dialog so to click on the desired element you need to induce WebDriverWait for the element to be clickable and you can use the following solutions:
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()
这篇关于在 Selenium Python 中关闭浏览器弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文