简体   繁体   English

如何使用 Selenium Python 在 Chromedriver 中管理/提醒消息

[英]How to manage/alerts message in Chromedriver with Selenium Python

I am using Selenium to get Magnet links from a website, but when I click on the Chrome Webdriver link it asks me if I want to open the magnet link in uTorrent (Open utorrent?), and that alert has two buttons, Open uTorrent and Cancel as you can see in the picture.我正在使用 Selenium 从网站获取磁力链接,但是当我单击 Chrome Webdriver 链接时,它会询问我是否要在 uTorrent 中打开磁力链接(打开 utorrent?),并且该警报有两个按钮, Open uTorrent和如图所示, Cancel 在此处输入图像描述

I want to know how to manage,我想知道如何管理

1- if can I Send Keys, to chrome to press LEFT_ARROW and ENTER and Send the link to uTorrent 1-如果我可以发送密钥,到 chrome 按LEFT_ARROWENTER并将链接发送到 uTorrent

2- Get/take the alert with selenium in some way and press "Open uTorrent" Alert = Driver.Alerts().ClickOK() 2- 以某种方式使用 selenium 获取/接收警报,然后按“打开 uTorrent” Alert = Driver.Alerts().ClickOK()

3- Change the settings for Chrome webdriver to disable this alert and send all the links with no alerts. 3- 更改 Chrome webdriver 的设置以禁用此警报并发送所有没有警报的链接。

4- Or any other idea... 4-或任何其他想法...

All this in Python.所有这些都在 Python 中。 Using Chrome Webdriver on Windows 10.在 Windows 10 上使用 Chrome Webdriver。

######### 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()
######### 

Here is the full code.这是完整的代码。

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()

It might not be exactly what you're looking for, but you can save the magnet link in a string, then use os.startfile to open it in your torrernt client:它可能不是您正在寻找的,但您可以将磁力链接保存在一个字符串中,然后使用os.startfile在您的 torrennt 客户端中打开它:

os.startfile(magnet_link)

alert = driver.switch_to_alert()
alert.accept()

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

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