python selenium句柄弹出警报

2hh7jdfx  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(382)

所以我正在尝试制作这个程序,我让它在nyaa.si上搜索一部带有过滤器之类的动画,我想给它增加一点趣味。长话短说,当我点击磁铁按钮时,它总是要求我提示允许qbittorrent打开磁铁链接。图片代码如下,但重要的部分在它的末尾,我得到的错误是:

selenium.common.exceptions.NoAlertPresentException: Message: no such alert

代码:

from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    import json
    from time import sleep
    from selenium.webdriver.common.alert import Alert

headless = False

path = 'C:\\chromedriver.exe'
options = webdriver.ChromeOptions()
if input('headless?: ') != '':
    headless = True
    options.add_argument('headless')

with open('urls.json', 'r') as f:
    data = json.load(f)

driver = webdriver.Chrome(path, options=options)
url = data['urls']['nyaa']
driver.get(url)

if headless == False:
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'themeToggle'))).click()

driver.find_element_by_class_name('navbar-toggle.collapsed').click()

driver.find_elements_by_class_name('form-control')[0].send_keys('Bleach')

temp = driver.find_elements_by_css_selector('select.form-control')
_filter = temp[0]
_category = temp[1]

_filter.click()

driver.find_element_by_css_selector('select.form-control[title=Filter] option[title="Trusted only"]').click()

_category.click()
driver.find_element_by_css_selector('select.form-control[title=Category] option[value="1_2"]').click()
driver.find_element_by_css_selector('button.btn.btn-primary.form-control').click()

temp = driver.find_elements_by_css_selector('tr.success td.text-center i.fa.fa-fw.fa-magnet')
temp[0].click()
egdjgwm8

egdjgwm81#

我在你的代码中没有看到任何关于警报的信息,只有导入。在磁铁后面应该有这样的东西。单击()

driver.switch_to.alert().accept(); # or dismiss() if you dont want to accept

相关问题