Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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在Chromedriver中管理/警报消息_Python_Selenium_Selenium Chromedriver - Fatal编程技术网

如何使用Selenium Python在Chromedriver中管理/警报消息

如何使用Selenium Python在Chromedriver中管理/警报消息,python,selenium,selenium-chromedriver,Python,Selenium,Selenium Chromedriver,我正在使用Selenium从网站获取磁铁链接,但当我单击Chrome Webdriver链接时,它会询问我是否要在uTorrent(打开uTorrent?)中打开磁铁链接,该警报有两个按钮,打开uTorrent和取消,如图所示 我想知道如何管理 1-如果我可以发送按键,请按chrome上的左箭头和回车,然后将链接发送到“自动更新” 2-以某种方式使用selenium获取/接收警报,然后按“打开自动更新”alert=Driver.Alerts()。单击OK() 3-更改Chrome webdriv

我正在使用Selenium从网站获取磁铁链接,但当我单击Chrome Webdriver链接时,它会询问我是否要在uTorrent(打开uTorrent?)中打开磁铁链接,该警报有两个按钮,
打开uTorrent
取消
,如图所示

我想知道如何管理

1-如果我可以发送按键,请按chrome上的
左箭头
回车
,然后将链接发送到“自动更新”

2-以某种方式使用selenium获取/接收警报,然后按“打开自动更新”
alert=Driver.Alerts()。单击OK()

3-更改Chrome webdriver的设置以禁用此警报,并发送所有没有警报的链接

4-或任何其他想法

所有这些都是用Python实现的。在Windows10上使用ChromeWebDriver

######### here is my attempt to manage the alert/notification
alert = driver.switchTo().alert();
alert.send_keys(Keys.ARROW_LEFT)
print("Left")
alert.send_keys(keys.ENTER)
print("ENTER")

print('Send it to uTorrent')
t.sleep(10)
# Close the tab with URL B
driver.close()
######### 
这是完整的代码

from selenium import webdriver
import time as t
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

sobrecarga = "El servidor se encuentra sobrecargado en estos momentos"
acceder = "acceder"
driver = webdriver.Chrome()
driver.get("https://www.epublibre.org") 

ps = driver.page_source
t.sleep(2)
if sobrecarga in ps:
    print("Overcharge = " + str(sobrecarga in ps))
    #the page is overcharge
    UserName = driver.find_element_by_xpath('/html/body/div/div[1]/form/table/tbody/tr[3]/td[1]/input')
    UserName.send_keys('USER')
    PwdUser = driver.find_element_by_xpath('/html/body/div/div[1]/form/table/tbody/tr[3]/td[2]/input')
    PwdUser.send_keys('PASSWORD')
    Entrar = driver.find_element_by_xpath('/html/body/div/div[1]/form/table/tbody/tr[3]/td[3]/input[2]')
    Entrar.click()
    t.sleep(2)
elif acceder in ps:
    print("Acceder = " + str(acceder in ps))
    t.sleep(2)
    #Click "Acceder" link
    GetAccess = driver.find_element_by_xpath('/html/body/div/div[1]/div/div/a[2]')                  
    GetAccess.click()
    t.sleep(2)
    UserName = driver.find_element_by_xpath('//*[@id="txt_user"]')
    UserName.send_keys("USER")
    PwdUser = driver.find_element_by_xpath('//*[@id="txt_pass"]')
    PwdUser.send_keys("PASSWORD")
    Entrar = driver.find_element_by_xpath('//*[@id="login"]/div[6]/div/input')
    Entrar.click()

t.sleep(2)
#go to home page
HomePage = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/a')
HomePage.click()
#goto novedades
novedades = driver.find_element_by_xpath('/html/body/div/div[6]/div[1]/table/tbody/tr/td[1]/div[1]/div/span/a')
novedades.click()
t.sleep(2)

#Every page (inside the webpage) has several links, that I want to click
for i in range(1,5): #I want to see just the first 5 pages 
    for j in range(0,18): #Go for every link inside the page inthe webpage
        myBook = driver.find_element_by_xpath('/html/body/div/div[7]/div/div[2]/div[' + str(j+1) + ']/div/a') #take the link, are 18 in total

        libro = myBook.get_attribute('href') #get the href (the link itself)
        print('Este es el link: ' + libro) # just a notification for me
        # Open a new tab
        driver.execute_script("window.open('');")
        # Switch to the new tab and 
        driver.switch_to.window(driver.window_handles[1])
        #open URL "libro"
        driver.get(libro)

        #CLICK TO SEND TO UTORRENT (ATTEMPT)
        t.sleep(2)
        #there is a magnet link in the new webpage
        MagnetLink = driver.find_element_by_xpath('//*[@id="en_desc"]')
        #and when click the magnet link chrome ask me "Open uTorrent"
        MagnetLink.click()
        t.sleep(10)


        ######### here is my attempt to manage the alert/notification
        alert = driver.switchTo().alert();
        alert.send_keys(Keys.ARROW_LEFT)
        print("Left")
        alert.send_keys(keys.ENTER)
        print("ENTER")

        print('Send it to uTorrent')
        t.sleep(10)
        # Close the tab with URL B
        driver.close()
        ######### 


        # Switch back to the first tab with URL A
        driver.switch_to.window(driver.window_handles[0])
        print("Current Page Title is : %s" %driver.title)

    siguiente = driver.find_element_by_xpath('/html/body/div/div[8]/div/div/ul/li[6]/a') #/html/body/div/div[8]/div/div/ul/li[6]/a #//*[@id="pagina"]/a
    siguiente.click()

它可能不是您想要的,但您可以将磁铁链接保存为字符串,然后使用在torrernt客户端中打开它:

os.startfile(磁铁链接)

设置意外警报行为可能有助于。。。注意:“notify”仅表示抛出异常,这发生在下一个webdriver操作发生之前:(在ChromeDriver选项中设置)
alert = driver.switch_to_alert()
alert.accept()