On the website mentioned below, when I open it using chromedriver and in my extract_data() function inside my try-except block, I attempt to dismiss a pop-up message by clicking the ‘x’ button. However, it clicks the wrong button.
Interestingly, when I’m in debug mode and put a breakpoint on the line with the variable close_button inside my try block, and manually wait for the pop-up message to appear in my browser, then I run the next line, it identifies the ‘x’ button correctly and properly dismisses the message.
I’m stumped on how to fix this issue so that my program can properly identify and close the ‘x’ button, instead of some other tag with the label ‘button’. I’ve tried getting specific with By.TAG_NAME and failed on multiple attempts using other methods such as By.ID, By.CLASS_NAME, By.CSS_SELECTOR, etc.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
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
from selenium.webdriver.common.action_chains import ActionChains
import PySimpleGUI as sg
from pathlib import Path
import pandas as pd
url = r"https://simpletire.com/brands/american-tourer-tires/touring-a-s#badge=Best%20rated&curationPos=none&curationSeq=none&curationSource=none&delivery=Del3&itemId=208786&mpn=AMSYTH0258&pageSource=PDP&pick=best-rated&productPos=none&rad=BE®ion=r2&tireSize=195-55r15&userRegion=2&userZip=19422&v=1&zip=19422"
service = r"somefilepath"
filename = "simple_tire_data"
def extract_data(url, service, output_folder, filename):
"""Main function for extracting data from simpletire.com and exporting to excel.
Parameters:
url -- the url of the page with the drop-down list of tire options
service -- the absolute folder path to your chromedriver.exe on your PC. Please ensure you have the version downloaded
that matches your chrome web browser version. You can download the chromedriver.exe from
this site: https://chromedriver.chromium.org/downloads
output_folder -- the folder where you want to store the output excel file
filename -- the name of the output excel file
"""
# use selenium to open URL with chrome driver
driver = get_driver(url, service)
# wait for pop-up to appear
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@id='attentive_overlay']")))
# find and click "X" button to close pop-up
try:
close_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.TAG_NAME, "button")))
actions = ActionChains(driver)
actions.move_to_element(close_button).click().perform()
except:
print("Pop up message was not able to be dismissed.")
# find the dropdown list and loop through the options
dropdown = driver.find_element(By.XPATH, "//*[@id='selectSizeButton']")
options = dropdown.find_elements(By.TAG_NAME, "span")
tire_data = {}
# for option in options:
# option_text = option.text
# option.click()
# # get data from tech specs container
# tech_specs_data = scrape_tech_specs_data(driver)
# # store data in nested dictionary
# tire_data[option_text] = tech_specs_data
# print(tire_data)
# driver.quit()
# # convert dictionary to dataframe
# df = pd.DataFrame.from_dict(tire_data, orient="index")
# # export dataframe to excel
# output_path = Path(output_folder, f"{filename}.xlsx")
# df.to_excel(output_path, index=True)
def get_driver(url, service):
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
service = Service(rf"{service}")
chrome_browser = webdriver.Chrome(service=service, options=chrome_options)
chrome_browser.get(rf"{url}")
chrome_browser.maximize_window()
return chrome_browser
extract_data(url, service, output_folder, filename)
2
Answers
BUTTONs are very common on many web pages. You need to use a more specific locator so that you can find that BUTTON, and only that BUTTON uniquely. When creating a locator, we’re looking for something unique about the element. Sometimes you will find an
id
orname
attribute. Those are usually the best candidates. In this case, from the HTML you posted we can see that the BUTTON you are trying to click has an ID. So a simple locator change to the below should work.I see an IFRAME within which this close button element is wrapped. Try switching into the IFRAME first, use below code:
After that try clicking on the close button, use code/locator provided by Jeff C:
Once pop-up is closed, do not forget to switch back to main content, use below code: