I am currently working on a project where I am automating whatsapp messages through url. like this https://wa.me/number_here
whenever i do it normally everything goes fine, but when I try to automate this a whatsapp popup box appears and blocks everything, I mean everything, no right-click no developer options, that is why i cant get the x path of the button on that popup. i have tried (driver.shift_to,alert.close)but it says that there is no such alert. i have tried to find the button by contains(text) method but also did not work. here is my code.
chrome_options = Options() chrome_options.add_argument('--user-data-dir = user-data') chrome_options.add_experimental_option('excludeSwitches', ['enable-automation']) chrome_options.add_argument('--disable-notifications') chrome_options.add_argument('--disable-popup-blocking') chrome_options.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=chrome_options, executable_path= driver_path) wait = WebDriverWait(driver, 60) contact = f'https://wa.me/{number}' driver.get(contact) time.sleep(3) alert = wait.until(EC.alert_is_present()) alert.accept()
please help me how to bypass this popup. thanks
Advertisement
Answer
I think, this link can be helpful for you: https://stackoverflow.com/a/19019311/12000849
What I do is to set a conditional delay with WebDriverWait just before the point I expect to see the alert, then switch to it, like this:
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException browser = webdriver.Firefox() browser.get("url") browser.find_element_by_id("add_button").click() try: WebDriverWait(browser, 3).until(EC.alert_is_present(), 'Timed out waiting for PA creation ' + 'confirmation popup to appear.') alert = browser.switch_to.alert alert.accept() print("alert accepted") except TimeoutException: print("no alert")