Reputation: 1716
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.
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
2- Get/take the alert with selenium in some way and press "Open uTorrent" Alert = Driver.Alerts().ClickOK()
3- Change the settings for Chrome webdriver to disable this alert and send all the links with no alerts.
4- Or any other idea...
All this in Python. Using Chrome Webdriver on Windows 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()
#########
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()
Upvotes: 0
Views: 687
Reputation: 673
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(magnet_link)
Upvotes: 1