Python Selenium Upload a file via Windows Upload - python

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

Related

How can I open a new tab with selenium python?

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.

selenium firefox webdriver doesn't make a complete download of a PDF with python

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.

copying text selenium using .text()

I need to scrape some text from a table, using selenium. I'm a complete novice, but thanks to Stack overflow, I was able to get to get pretty far. Now I am getting an error when using text() to copy text. Here are my codes.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
driver = webdriver.Chrome('C:/Users/User/Downloads/chromedriver')
url = "https://aca.tampagov.net/citizenaccess/Default.aspx#"
driver.get(url)
# The website I need to check
# Click "search" at the top menu
driver.find_element_by_xpath('//*[#id="main_menu"]/ul/li[2]/a').click()
# Click "building permits" in the menu that follows below
driver.find_element_by_xpath('//[#id="main_menu"]/ul/li[2]/ul/li[1]/a').click()
#change iframes
driver.switch_to.frame("ACAFrame")
# When changing iframes, I was recommended that I should use this,
# WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//*[#id='ACAFrame']")))
# but I'm now having troubles running this line so I changed it to the line like above
Now you can find that there's a table below. I want to copy text using a code such as:
driver.find_element_by_xpath('//tbody/tr[3]/td[4]').text()
But I get, instead:
Traceback (most recent call last):
File "<ipython-input-117-2a4c7f9321b5>", line 1, in <module>
driver.find_element_by_xpath('//tbody/tr[3]/td[4]').text()
TypeError: 'str' object is not callable
What am I doing wrong here?
Use
driver.find_element_by_xpath('//tbody/tr[3]/td[4]').text
instead of
driver.find_element_by_xpath('//tbody/tr[3]/td[4]').text()
The statement driver.find_element_by_xpath('//tbody/tr[3]/td[4]').text returns a string, so you don't have to add the parenthesis at the end. If you add the parenthesis, then syntax means you are calling it, but you can only call callable objects, such as functions. Thus, the error.

Save As Dialog on Iexplorer How to save htm file

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.

What is correct way to set send keys when use selenium and python

I'm new python (programming). Currently working on a project to fill up forms using selenium and python.
So for everything seems okay. I successfully installed python 3.6, selenium on pycharm and chrome drive on my windows.
I try to open a website and fill the form using the script. The script opens the site but couldn't put the texts in the form.
I have been browsing stack overflow and googling for hours and anything doesn't work.
It shows error regarding sendkeys.
Here's the full code
from selenium import webdriver
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
# Replace below path with the absolute path
# to chromedriver in your computer
driver = webdriver.Chrome('C:\\Users\public\chromedriver.exe')
# Go to your page url
driver.get('https://www.website.com/')
# Get button you are going to click by its id ( also you could us find_element_by_css_selector to get element by css selector)
button_element = driver.find_element_by_class_name('signup_email_link')
button_element.click()
input_element = driver.find_element_by_name("first_name")
input_element.sendkeys(ram)
Here's the error code,
C:\Users\user\AppData\Local\Programs\Python\Python36-32\python.exe "C:/Users/user/PycharmProjects/My Projects/My scripts automate.py"
Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/My Projects/My scripts automate.py", line 22, in <module>
input_element.sendkeys(ram)
AttributeError: 'WebElement' object has no attribute 'sendkeys'
Process finished with exit code 1
I even tried to replace the input_element with web_element but error continuous.
I use latest Python 3.6 version on pycharm.
Use
input_element.send_keys(ram)
instead of
input_element.sendkeys(ram)
It should be input_element.send_keys(ram).
List of methods here.

Categories

Resources