Close browser popup in Selenium Python

2024/10/11 20:25:18

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/37browser = 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']"}
Answer

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 ECoptions = 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()
https://en.xdnf.cn/q/118281.html

Related Q&A

How can I replace certain string in a string in Python?

I am trying to write two procedures to replace matched strings in a string in python. And I have to write two procedures. def matched_case(old new): .........note: inputs are two strings, it returns a…

Python: `paste multiple (unknown) csvs together

What I am essentially looking for is the `paste command in bash, but in Python2. Suppose I have a csv file:a1,b1,c1,d1 a2,b2,c2,d2 a3,b3,c3,d3And another such:e1,f1 e2,f2 e3,f3I want to pull them toget…

Django Redirect after Login Error

Im new to Django and I know that to redirect after login I have to set the parameter page. But this only works when the login is successful. How can i do the same thing when some error occurs?? Ps: I…

Python: Pulling .png from a website, outputting to another

Hoping this question is not too vague, or asking for too much. Essentially I am analyzing large amounts of spectra, and wanting to create one large webpage that contains these spectra rather than looki…

Using peakutils - Detecting dull peaks

Im currently using peakutils to find peaks in some data. My data contains some "dull peaks", that is my peaks plateau somewhat. I cant set my code to find these peaks even after playing aroun…

nonlinear scaling image in figure axis matplotlib

enter image description hereI hope I have not over-looked as previously asked question. I dont think so. I have an image of a spectrum. I have several laser lines for calibration. Since the laser li…

How to correctly call a git submodule symlinked?

On the Sublime Text Package Control issue:Why ignore VCS-based packages accordingly to this message? I find out what causes this error. I had the package All Autocomplete triggering it. Then I gone to…

Python and MySQL query with quotes

With a script in Python3, after extracting some strings from a file, they should be used as data to be inserted into a MySQL database as follows:query1 = """INSERT INTO {:s} VALUES ({:s}…

Using scipy kmeans for cluster analysis

I want to understand scipy.cluster.vq.kmeans. Having a number of points distributed in 2D space, the problem is to group them into clusters. This problem came to my attention reading this question and …

Scrapy and celery `update_state`

I have the following setup (Docker):Celery linked to Flask setup which runs the Scrapy spider Flask setup (obviously) Flask setup gets request for Scrapy -> fire up worker to do some workNow I wish …