Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
889 views
in Technique[技术] by (71.8m points)

handle popups in selenium python

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() 

enter image description here

please help me how to bypass this popup. thanks

question from:https://stackoverflow.com/questions/65924841/handle-popups-in-selenium-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...