问题
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']"}
回答1:
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()
回答2:
You should wait for the popup to close it:
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
url = "https://shopping.rochebros.com/shop/categories/37"
browser = webdriver.Chrome(executable_path=chromedriver, options=options)
browser.get(url)
wait(browser, 10).until(EC.element_to_be_clickable((By.ID, "shopping-selector-parent-process-modal-close-click"))).click()
If popup might not appear, you can use try
/except
to just move on if no popup appeared within 10 seconds:
from selenium.common.exceptions import TimeoutException
try:
wait(browser, 10).until(EC.element_to_be_clickable((By.ID, "shopping-selector-parent-process-modal-close-click"))).click()
except TimeoutException:
print("No popup...")
来源:https://stackoverflow.com/questions/52598793/close-browser-popup-in-selenium-python