简体   繁体   English

硒警报处理 Python

[英]Selenium Alert Handling Python

So I am using Selenium to simply go to a given list of websites and take a screenshot of them.所以我使用 Selenium 只是简单地转到给定的网站列表并截取它们的屏幕截图。 However I have run into some having alerts and some do not.但是,我遇到了一些有警报的情况,而有些则没有。 I am looking to do something along the lines of if alert then this else keep moving.我希望做一些类似于 if alert then this else 继续前进的事情。

Here is my current code这是我当前的代码

from selenium import webdriver
import pandas as pd
import time

path = "C:/Users/dge/Desktop/Database/NewPythonProject/html Grabber/Output/Pics/"
df = pd.DataFrame()
df = df.append(pd.read_csv('crime.csv'), ignore_index=True)
driver = webdriver.Chrome('C:/Users/dge/Desktop/Database/NewPythonProject/html Grabber/chromedriver.exe')

for i in df.index:
    print(i)
    pic = path + df['site'][i] + '.png'
    driver.get('http://' + df['site'][i])
    time.sleep(5)
    driver.save_screenshot(pic)

I've seen this but just not sure how to add it in the loop我见过这个,但只是不知道如何将它添加到循环中

driver.find_element_by_id('').click()
alert = driver.switch_to_alert()

The best way to put this may be to say ignore any errors and continue on through my list of urls.最好的方式可能是说忽略任何错误并继续浏览我的网址列表。

JavaScript can create alert() , confirm() or prompt() JavaScript 可以创建alert()confirm()prompt()

To press button OK按下按钮OK

driver.switch_to.alert.accept()  # press OK

To press button CANCEL (which exists only in confirm() and prompt() )按下按钮CANCEL (仅存在于confirm()prompt()

driver.switch_to.alert.dismiss()  # press CANCEL

To put some text in prompt() before accepting it在接受之前在prompt()放入一些文本

prompt = driver.switch_to.alert
prompt.send_keys('foo bar')
prompt.accept()

There is no function to check if alert is displayed没有检查是否显示警报的功能
but you can put it in try/except to catch error when there is no alert.但是您可以将它放在try/except以在没有警报时捕获错误。

try:
    driver.switch_to.alert.accept()  # press OK
    #driver.switch_to.alert.dismiss()  # press CANCEL
except Exception as ex:
    print('Exception:', ex)

Minimal working example.最小的工作示例。

Because I don't know page which displays alert so I use execute_script to display it.因为我不知道显示警报的页面,所以我使用execute_script来显示它。

from selenium import webdriver
import time

#driver = webdriver.Firefox()
driver = webdriver.Chrome()

driver.get('http://google.com')

# --- test when there is alert ---

driver.execute_script("console.log('alert: ' + alert('Hello World!'))")
#driver.execute_script("console.log('alert: ' + confirm('Hello World!'))")
#driver.execute_script("console.log('alert: ' + prompt('Hello World!'))")
time.sleep(2)

try:
    driver.switch_to.alert.accept()  # press OK
    #driver.switch_to.alert.dismiss()  # press CANCEL
except Exception as ex:
    print('Exception:', ex)

# --- test when there is no alert ---

try:
    driver.switch_to.alert.accept()  # press OK
    #driver.switch_to.alert.dismiss()  # press CANCEL
except Exception as ex:
    print('Exception:', ex)

# ---

driver.save_screenshot('image.png')
driver.close()

BTW: if you want to first try to press CANCEL and when it doesn't work then press OK顺便说一句:如果您想先尝试按CANCEL ,当它不起作用时,请按OK

try:
    driver.switch_to.alert.dismiss()  # press CANCEL
except Exception as ex:
    print('Exception:', ex)
    try:
        driver.switch_to.alert.accept()   # press OK
    except Exception as ex:
        print('Exception:', ex)

BTW: different problem can be popup notifications or geolocation alerts顺便说一句:不同的问题可以是弹出通知或地理位置警报

How to click Allow on Show Notifications popup using Selenium Webdriver 如何使用 Selenium Webdriver 在“显​​示通知”弹出窗口中单击“允许”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM