I hope you're doing well!
I want to scrap this website https://proadvisor.intuit.com/app/accountant/search?region=ca&distance=1000, if you click on it the site load normally but if I run my script appears something like a "pop-up" window (I attached the evidence).
I'm using Selenium and Python to do it. I want to do click on the 'x' button to close that and then I can start to scrap the website so I tried with the next two ways with the XPATH:
close_button = driver.find_element(By.XPATH, '/html/body/div[4]/button')
close_button.click()
driver.find_element(By.XPATH, '//div[contains(@class,"coach-marks")]//button[@aria-label="close"]').click()
But the result was the same, the next error is launched and the "pop-up" window doesn't close.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[4]/button"}
The HTML code is:
<button class="a--close_button" aria-label="close" tabindex="0">
<svg class="svg--close_button" width="19" height="19" viewBox="0 0 19 19">
<path
fill-rule="evenodd"
d="M19 1.83L17.17 0 9.5 7.67 1.83 0 0 1.83 7.67 9.5 0 17.17 1.83 19l7.67-7.67L17.17 19 19 17.17 11.33 9.5z"
></path>
</svg>
</button>
All my Python code is:
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.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
driver.get("https://proadvisor.intuit.com/app/accountant/search?region=ca&distance=1000")
# click to close button
close_button = driver.find_element(By.XPATH, '/html/body/div[4]/button')
close_button.click()
If you can help me I will very grateful with you.
Regards.
Wait for the element to become visible before trying to interact with it. You can do this by using an explicit wait with the expected_conditions module from "selenium.webdriver.support.ui".
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.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
driver.get("https://proadvisor.intuit.com/app/accountant/search?region=ca&distance=1000")
# wait for the close button to be visible and click it
wait = WebDriverWait(driver, 10)
close_button = wait.until(EC.visibility_of_element_located((By.XPATH, '//button[@aria-label="close"]')))
close_button.click()