Reputation: 645
I am trying to scrape from a website and while scraping a popup window comes up and I am unable to close it.
I tried to isolate the X button but it shows up, element not clickable.
Is there any dynamic way I can check for the popup and close it as soon as it comes up?
Here's the link to the site.
This is how the popup comes up:
and this is my code:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
from datetime import datetime
import time
import re
import logging
import traceback
from datetime import datetime, timedelta
import argparse
import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy import exc
import mysql.connector as mysql
import io
import re
options = Options()
#options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--window-size=1366,768")
options.add_argument("--disable-notifications")
options.add_experimental_option('prefs', {'intl.accept_languages': 'en_GB'})
#options.headless = True
url='https://www.sleepx.com/ortho-memory-foam-mattress'
driver = webdriver.Chrome(options=options)
driver.get(url)
driver.find_element_by_xpath('/html/body/div[1]/div/div/div').click() # popup close button by XPath
Upvotes: 1
Views: 509
Reputation: 33351
You should use explicit waits provided by Selenium WebDriverWait to wait for the element visibility or clickability and then click it.
Like this:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
from datetime import datetime
import time
import re
import logging
import traceback
from datetime import datetime, timedelta
import argparse
import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy import exc
import mysql.connector as mysql
import io
import re
options = Options()
#options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--window-size=1366,768")
options.add_argument("--disable-notifications")
options.add_experimental_option('prefs', {'intl.accept_languages': 'en_GB'})
#options.headless = True
url='https://www.sleepx.com/ortho-memory-foam-mattress'
driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 20)
driver.get(url)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.WigzoCloseButton"))).click()
Upvotes: 2