Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium警报处理Python_Python_Selenium - Fatal编程技术网

Selenium警报处理Python

Selenium警报处理Python,python,selenium,Python,Selenium,因此,我使用Selenium只需访问给定的网站列表并截图。然而,我遇到了一些有警报,而有些没有。我期待着做一些事情沿线的如果警报,然后这其他继续移动 这是我目前的代码 from selenium import webdriver import pandas as pd import time path = "C:/Users/dge/Desktop/Database/NewPythonProject/html Grabber/Output/Pics/" df = pd.DataFrame()

因此,我使用Selenium只需访问给定的网站列表并截图。然而,我遇到了一些有警报,而有些没有。我期待着做一些事情沿线的如果警报,然后这其他继续移动

这是我目前的代码

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)
我见过这个,但不知道如何将它添加到循环中

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

最好的方法是说忽略任何错误并继续浏览我的URL列表。

JavaScript可以创建
alert()
confirm()
prompt()

按下按钮
OK

driver.switch_to.alert.accept()  # press 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)
要按按钮
取消
(该按钮仅存在于
确认()
提示()

在接受前在
prompt()
中放入一些文本

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

没有检查是否显示警报的功能
但您可以将其放入
try/except
中,以便在没有警报时捕获错误

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

最少的工作示例

因为我不知道哪个页面显示警报,所以我使用
execute\u 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()

顺便说一句:如果您想先尝试按
取消
,当它不起作用时,再按
确定

driver.switch_to.alert.accept()  # press 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)

顺便说一句:不同的问题可能是弹出通知或地理位置警报


JavaScript可以创建
alert()
confirm()
prompt()

按下按钮
OK

driver.switch_to.alert.accept()  # press 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)
要按按钮
取消
(该按钮仅存在于
确认()
提示()

在接受前在
prompt()
中放入一些文本

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

没有检查是否显示警报的功能
但您可以将其放入
try/except
中,以便在没有警报时捕获错误

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

最少的工作示例

因为我不知道哪个页面显示警报,所以我使用
execute\u 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()

顺便说一句:如果您想先尝试按
取消
,当它不起作用时,再按
确定

driver.switch_to.alert.accept()  # press 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)

顺便说一句:不同的问题可能是弹出通知或地理位置警报


将其放在循环中的任何位置,看看会发生什么。当您收到错误消息时,请阅读并尝试解决它。简而言之:第一次尝试,下一次登录谷歌,最后问一个问题。@furas谢谢你的建议,但这正是我所要求的帮助。我想不出如何将其包含在循环中。首先,简单地将代码复制并粘贴到循环中并运行它。若它不起作用,那个么你们至少会得到一条错误信息,你们可以用它在谷歌上搜索。然后,您还可以在不同的位置或以不同的顺序测试代码。通过这种方式,我们可以通过在不同的配置中测试代码来学习。顺便说一下:您可以为显示警报的页面添加url,我们可以为测试代码添加页面。这是否回答了您的问题?把它放在循环中的任何地方,看看会发生什么。当您收到错误消息时,请阅读并尝试解决它。简而言之:第一次尝试,下一次登录谷歌,最后问一个问题。@furas谢谢你的建议,但这正是我所要求的帮助。我想不出如何将其包含在循环中。首先,简单地将代码复制并粘贴到循环中并运行它。若它不起作用,那个么你们至少会得到一条错误信息,你们可以用它在谷歌上搜索。然后,您还可以在不同的位置或以不同的顺序测试代码。通过这种方式,我们可以通过在不同的配置中测试代码来学习。顺便说一下:您可以为显示警报的页面添加url,我们可以为测试代码添加页面。这是否回答了您的问题?