Reputation: 4229
I want to close the popup window that appears when I hit a particular url. Here is the "inspect element" window of that page:
Here is what I have tried:
driver.find_element_by_css_selector("i[@class='popupCloseIcon']").click()
But it gives following error:
InvalidSelectorException: Message: Given css selector expression "i[@class='popupCloseIcon']" is invalid: InvalidSelectorError: Document.querySelector: 'i[@class='popupCloseIcon']' is not a valid selector: "i[@class='popupCloseIcon']"
Here is the url where popup appears: https://www.investing.com/equities/oil---gas-dev-historical-data Once the url is opened through selenium, the pop up appears after a few seconds. How can I click that close button?
Upvotes: 0
Views: 2868
Reputation: 193358
As the pop up appears after a few seconds accessing the url https://www.investing.com/equities/oil---gas-dev-historical-data to to close the popup window you need to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies:
Using CSS_SELECTOR
:
WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "i.popupCloseIcon.largeBannerCloser"))).click()
Using XPATH
:
WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.XPATH, "//i[@class='popupCloseIcon largeBannerCloser']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Upvotes: 1
Reputation: 7563
The popup appears after some time, so you need wait to solve this. And you have invalid selector : i[@class='popupCloseIcon']
, please use i[class*='largeBannerCloser']
Try the below:
driver.get('https://www.investing.com/equities/oil---gas-dev-historical-data')
try:
popup = WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "i[class*='largeBannerCloser']")))
popup.click()
except TimeoutException as to:
print(to)
This is wait until 60 seconds maximum.
Following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
Upvotes: 2
Reputation: 22
First, "i[@class='popupCloseIcon']" is a invalid css selector locator, it should be "i[class='popupCloseIcon']". Second, there are four elements mapped to the "i[class='popupCloseIcon']", the css selector "div.right>i.popupCloseIcon" will help you to locate the target element
Upvotes: 0