I have a case like this: I crawl one website but the website have a popup show random when the website show a popup I need it to wait 180 seconds. I want to check the condition popup and click to button for turn-off popup.
I want to ask: How to check the condition and click the button in popup with Selenium and Python but not have slow. I was can do that with my code but the time to find_element was too slow, it loses about 15-30 seconds for delay. This is my code:
The popup is random soo if when it does not show the popup. It will lose a lot of time in row find_element.
This is first code:
try:
print('Try find Alert box')
checkup_click_understand = driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/div/div[3]/button')
if checkup_click_understand.is_displayed():
actions = webdriver.ActionChains(driver)
actions.click(checkup_click_understand)
actions.perform()
time.sleep(180)
except NoSuchElementException:
print('No Alert BOX')
I tried another way but it has the same problem. This is the second code:
try:
print('Try find Alert box')
# checkup_click_understand = WebDriverWait(driver, 3).until(
# EC.presence_of_element_located((By.XPATH, "/html/body/div[4]/div[3]/div/div[3]/button"))
# )
wait = WebDriverWait(driver, 1)
checkup_click_understand = wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[4]/div[3]/div/div[3]/button')))
print(checkup_click_understand.is_displayed())
if checkup_click_understand.is_displayed():
actions = webdriver.ActionChains(driver)
actions.click(checkup_click_understand)
actions.perform()
time.sleep(180)
except:
print('No Alert BOX')
And the third code. I try to get the size to condition, but it same problem, take a lot of time when the code run to checkup_click_understand = WebDriverWait(driver, 3). I was think the code will work and I only need 3 seconds to wait, but it not, I need to spend 15-30s like the first and the second way.
try:
print('Try find Alert box')
checkup_click_understand = WebDriverWait(driver, 3).until(
EC.element_to_be_clickable((By.XPATH, '/html/body/div[4]/div[3]/div/div[3]/button'))
)
if checkup_click_understand.size > 0:
actions = webdriver.ActionChains(driver)
actions.click(checkup_click_understand)
actions.perform()
time.sleep(180)
except TimeoutException:
print('No Alert BOX')