繁体   English   中英

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

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

我正在使用 Selenium 从网站获取磁力链接,但是当我单击 Chrome Webdriver 链接时,它会询问我是否要在 uTorrent 中打开磁力链接(打开 utorrent?),并且该警报有两个按钮, Open uTorrent和如图所示, Cancel 在此处输入图像描述

我想知道如何管理

1-如果我可以发送密钥,到 chrome 按LEFT_ARROWENTER并将链接发送到 uTorrent

2- 以某种方式使用 selenium 获取/接收警报,然后按“打开 uTorrent” Alert = Driver.Alerts().ClickOK()

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

4-或任何其他想法...

所有这些都在 Python 中。 在 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()
######### 

这是完整的代码。

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

它可能不是您正在寻找的,但您可以将磁力链接保存在一个字符串中,然后使用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