I am trying to save html file using webriver
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Ie("D:\\IEDriver\\IEDriverServer.exe")
driver.get("https://www.google.com/")
actions = ActionChains(driver)
actions.send_keys(Keys.LEFT_CONTROL + 's')
actions.perform()
"Save Webpage" window dialog popup so could you please advise how can I enter a file name and choose format (mth) and click save.
I know that it is possible to save html using urllib2 or import os, sys
from win32com but I need do it in this way. Actually chilkat.GetAndSaveMHT does not save in proper format.
You cannot enter a file name and choose a format using Selenium alone, because it cannot interact with the Save As system dialogue. You can follow this instructions to avoid the dialogue, or rely on some library - pywinauto and pywin32 for instance.
Related
I'm trying to make a program that opens multiple websites, but I can't get it to press control-t. I've tried multiple solutions, but I can't find one that works. When I do the keydown method, I get an error that says
webdriver has no attribute key_down
and when I try send_keys(Keys.CONTROL + 't') it doesn't raise any errors, or do anything. How can I open a new tab?
Here's my try :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://youtube.com")
search = driver.find_element_by_id("search")
#search.keydown(Keys.CONTROL)
#Webelement.key_down(Keys.CONTROL).send_keys('t').key_up(Keys.CONTROL).perform()
search.send_keys(Keys.CONTROL+'t')
time.sleep(10)
You can do it as
from selenium import webdriver
driver.get("https://www.youtube.com")
search = driver.find_element_by_id("search")
driver.execute_script("window.open('https://www.google.com')")
Here's what you can try :
from selenium import webdriver
webBrowser = webdriver.<>()
# This is first tab
webBrowser.get('<>')
# Second tab
webBrowser.execute_script("window.open('about:blank','secondtab');")
webBrowser.switch_to.window("secondtab")
webBrowser.get('<>')
# Third tab
webBrowser.execute_script("window.open('about:blank','thirdtab');")
webBrowser.switch_to.window("thirdtab")
webBrowser.get('<>')
You can learn more about it from Python – Opening multiple tabs using Selenium
Additionally I think you also have you look here.
However, if you want to run something on multiple windows, then I recommend having multiple instances of webdriver. It is much easier to manage, and is supported (There are workarounds on opening a new tab/window, such as pressing a hotkey that opens a new window, but they aren't supported).
You can use pyautogui to doy to open new tab using ctrl+t :
import pyautogui
pyautogui.keyDown('ctrl')
pyautogui.press('t')
pyautogui.keyUp('ctrl')
driver.switch_to.new_window()
From the webdriver docs
You can pass in 'tab' or 'window'. For ChromeDriver, it opens a tab by default.
I am trying to press Ctrl+s and then click enter on a webpage to save its html. But the multikey pressing functionality is not working out. I have tried way1 and way2 but both didn't work. However, If I do action.send_keys('s') by without executing action.key_down(Keys.CONTROL) before it, it works fine. This is my full code:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome(executable_path="chromedriver.exe")
# get method to launch the URL
driver.get("https://www.google.ca/")
time.sleep(3)
action = ActionChains(driver)
# click google search bar (to make sure driver is working)
driver.find_element_by_name("q").click()
print("passed1")
time.sleep(3)
# way 1
action.key_down(Keys.CONTROL).send_keys('s').key_up(Keys.CONTROL).perform()
print("passed2")
# way 2
action.key_down(Keys.CONTROL)
action.send_keys('s')
action.key_up(Keys.CONTROL)
action.perform()
print("passed2")
time.sleep(100)
driver.close()
Can someone please explain to me what is he issue? I've been trying to figure it out for an hour now.
After looking around, I found that it is not advisable to interact with the browser for this purpose. It is better to get the page_source instead.
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r"C:\Program Files (x86)\Selenium\chromedriver.exe")
driver.get("http://www.example.com")
with open('page.html', 'w', encoding='utf-8') as f:
f.write(driver.page_source)
I made a script which automates some processes using Selenium and another Script where I store variables like password and email.
I converted it to an a .exe file but the user needs to be able to edit the Details.py file.
Is there any work around since as soon as I convert it to .exe theres no way for me to edit and save files?
Edit: Posted code to help with an answer:
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from Details import *
driver = webdriver.Firefox()
url = "https://discordapp.com/channels/530588470905929729/538868623981412362"
driver.get(url)
email = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//input[#type='email']")))
email.send_keys(Email)
password = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//input[#type='password']")))
password.send_keys(Password + Keys.ENTER)
sleep(5)
textbox = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//textarea[#placeholder='Message #bot-commands']")))
textbox.send_keys("!work" + Keys.ENTER)
sleep(30)
driver.quit()
This is Details.py:
Password = "Password"
Email = "mail#mail.com"
You need to rethink the design of your program. It's not secure or user-friendly to edit .py files in order to alter saved credentials. I suggest you have a look around for already proven methods of storing sensitive information. There are lots of options out there and you can start with this post.
I'm trying to download a PDF with python, from a java event not html, and I already changed my firefox preferences, it makes the download but when I try to open the file it says that the file must be damaged or corrupt, I noted that when firefox webdriver makes the download, it doesn't download all the bytes of it so I don't know if it doesn't wait till the download is complete or if there's something I'm missing in my code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","application/pdf")
fp.set_preference("pdfjs.disabled",True)
fp.set_preference("browser.download.dir", "C:\\Users\\carlo\\Desktop\\
driver = webdriver.Firefox(firefox_profile=fp)
driver.get(the url which i cant give here)
Then I open a new window in the code and take control of it which is a PDF web window, and use this:
element= WebDriverWait(driver, 10).\until(EC.visibility_of_element_located((By.XPATH,"//*[#id='download']")))
element.click()
Additional to this, it doesn't have a URL; it's a java event that doesn't come with that and now it starts the download but it corrupts it. I have tried to wait with time.sleep but it still has the same problem. If there's a way to set a preference of direct download without opening a new window with the driver it should help, am I missing something?
I've already figured it out just needed to add time in the element.click.sleep(2)
In addition, you can verify if the file has been downloaded before quiting the browser:
import glob
import time
download_dir = "C:\\Users\\carlo\\Desktop"
def still_downloading(download_dir):
files = glob.glob(download_dir+"/*.part")
if len(files) > 0:
return True
return False
...
element.click()
while still_downloading(dl_location):
print "still downloading..."
time.sleep(1)
This way you don't have to "guess" the time needed to download your file beforehand.
I read a lot about and everyone advice to don't use the Windows Upload and write directly the path of my file; I even try to use some command found in the forum like:
swicthTo()
switch_to_window()
window_handles
but I didn't find any solution yet.
My main problem is that time I have no space to send directly the path of my file (see image below, the space to introduce the path is grey-out) but I have only the option to click in "Browse" and open the Windows Uploader:
Do you know how can I switch to the upload window's Windows and introduce my fie?
I try even in this way:
browse=wait(".//*[#id='fileinput']") #open the window upload
browse.click()
time.sleep(1)
def handle_dialog(element_initiating_dialog, dialog_text_input):
upload_dialog = driver.switch_to_active_element
print (upload_dialog)
upload_dialog.send_keys(dialog_text_input)
upload_dialog.send_keys(selenium.webdriver.common.keys.Keys.ENTER) # the ENTER key closes the upload dialog, other thread exits
handle_dialog(browse, "foobar.txt")
I find the windows and when I print I have this object:
We are already in the Location->Details Page <bound method WebDriver.switch_to_active_element of <selenium.webdriver.ie.webdriver.WebDriver (session="3e725bb7-40a7-452a-ad90-9cca1d41296a")>>
But after when I try to do send_keys I receive this error:
Traceback (most recent call last): File
"C:\Users\carlo.agresta\Desktop\IE - iQsonar.py", line 149, in
handle_dialog(browse, "foobar.txt") File "C:\Users\carlo.agresta\Desktop\IE - iQsonar.py", line 145, in
handle_dialog
upload_dialog.send_keys(dialog_text_input) AttributeError: 'function' object has no attribute 'send_keys'
I partially found a solution, I let behave my code as if the window upload is an alert so I do in this way:
browse=wait(".//*[#id='fileinput']")
browse.click()
time.sleep(1)
upload_dialog = driver.switch_to_alert()
print (upload_dialog)
upload_dialog.send_keys("C:\\Users\\carlo.agresta\\Desktop\\V4LabCredentials.csv")
Now my problem is that I can't accept accept and close the window :S any advice?
Thanks so much in advance
use autoit
example code:
import autoit
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
ActionChains(driver).move_to_element( driver.find_element_by_xpath("//div[#class='upload_button']")).click().perform()
handle = "[CLASS:#32770; TITLE:Open]"
autoit.win_wait(handle, 60)
autoit.control_set_text(handle, "Edit1", "\\file\\path")
autoit.control_click(handle, "Button1")