Upload window opened. need to enter file path by python - python

Hi team,
I have created program where I need to upload file. By clicking on "Upload" , I reach to this window. but here I stuck now. I need to enter path automatically. please help if you have any examples.
driver1 = webdriver.Chrome(executable_path= r'C:\\Users\\Ast1\\Desktop\\chromedriver.exe')
Sharepoint ="https://ast.sharepoint.com/sites/ExtTeam_GPS/Shared%20Documents/Forms/AllItems.aspx?viewid=3638f543%2D486c%2D4bca%2D85a7%2D8d86da0a8eb5&id=%2Fsites%2FExtTeam%5FNO%2FShared%20Documents%2FPowerBI%20Datasets"
driverLink= Sharepoint
driver1.get(driverLink)
Em = WebDriverWait(driver1,120).until(EC.presence_of_element_located((By.ID,'i0116')))
Em.send_keys("astemp1#ast.com")
elm =WebDriverWait(driver1,120).until(EC.presence_of_element_located((By.ID,'idSIButton9')))
elm.click()
driver1.implicitly_wait(100)
#elm1 = WebDriverWait(driver1,100).until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT,'Upload')))
elm2 = driver1.find_element_by_xpath('//*[#id="appRoot"]/div[1]/div[2]/div[3]/div/div[2]/div[2]/div[2]/div[1]/div/div/div/div/div/div/div[1]/div[2]/button')
elm2.click()
driver1.implicitly_wait(20)
elm3 = driver1.find_element_by_name('Files')
elm3.click()
driver1.implicitly_wait(20)
#Follwing code is not working.
Elm4 = driver1.find_element(By.ID('File name'))
Elm4.send_keys(r'C:\A1\AA1\M1\Input\TblGpsNew.xlsx')
#driver1.implicitly_wait(100)

If the page where you have the upload button (entire page), if the below xpath
//input[#type='file']
exists at least one time 1/1 matches then, you can directly send the keys.
Explicitly no need to click on upload button and then interact with the windows pop up.
driver1.find_element(By.XPATH("//input[#type='file']")).send_keys("C:\A1\AA1\M1\Input\TblGpsNew.xlsx")

Related

Python Selenium .click() Not Working but Server Response is 200

I'm having a hard time coming up with a solution. I'm writing code in Python using the Selenium library to pull reports from a site we utilize. In order to access the reports, it has to click on a link to expand so the code can find the report names. The issue I'm running into is the code is running fine and clicking on the link, but the link isn't expanding to reveal the reports. Debugging is saying that the link is found and clicked with a post response of 200, but nothing is happening. Due to the nature of the response code saying everything is fine, I'm at a loss of why the folder isn't expanding. Due to the nature of the site, I cannot share too much but have attached my code that I can share along with screenshots.
wait = WebDriverWait(driver_chrom, 30)
def Try_Wait(args, thing_string):
try:
wait.until(EC.presence_of_element_located(args))
logger_write.debug("found element "+thing_string)
#print("found args for ", thing_string)
return
except:
Exit_LogOut()
driver_chrom.quit()
sys.exit()
def Button_Click(args, thing_string):
try:
driver_chrom.find_element(*args).click()
logger_write.debug("Found button click"+thing_string)
#print('clicking button for ', thing_string)
return
except:
#print('did not click button ', thing_string)
logger_write.error("Did not find button "+thing_string)
return
thing_string = 'Opening folder for reports'
Try_Wait((By.XPATH,'//div[#id="folder1"]/table/tbody/tr//td[a[starts-with(#href, "javascript:clickOnNode(") and contains(#href, "1") and contains(text(),"Standard")]]'), thing_string)
Button_Click((By.XPATH,'//div[#id="folder1"]/table/tbody/tr//td[a[starts-with(#href, "javascript:clickOnNode(") and contains(#href, "1") and contains(text(),"Standard")]]'), thing_string)
This is what it looks like after code above runs
This is what it should look like so that reports are loaded into the html
Here is the inspect screenshot:
Try following xpath should work for you.
//a[text()="Standard"]
Instead this
//div[#id="folder1"]/table/tbody/tr//td[a[starts-with(#href, "javascript:clickOnNode(") and contains(#href, "1") and contains(text(),"Standard")]]
Or use Link_Text since it is anchor rag
Try_Wait((By.LINK_TEXT ,"Standard"), thing_string)
Button_Click((By.LINK_TEXT ,"Standard"), thing_string)

PywinAuto - Excel Automation Can not click the button

I m making an Excel automation via pywinauto library. But there is a hard challange for me due to using Excel Oracle add-ins called Smartview.
I need to click 'Private Connections' button, however i can't find any little info in app.Excel.print_control_identifiers() Private Connections
So i tried to use inspector.exe for find ui element regarding private connections button, however i couldn't find any little solvetion inside of inspector.exe's result inspector's result
Then i used another program called UISpy, however i can only find private connection's pane inside of the program. UISpy's result
i tried to find an answer but i couldn't find out anything. So, can you help me to click here?
By the way here is my code :
import pywinauto
from pywinauto import application
from pywinauto.keyboard import send_keys
from pywinauto.controls.common_controls import TreeViewWrapper
program_path = r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE"
file_path = r"C:\Users\AytugMeteBeder\Desktop\deneme.xlsx"
app = application.Application(backend="uia").start(r'{} "{}"'.format(program_path, file_path))
# sapp = application.Application(backend="uia").connect(title = 'deneme.xlsx - Excel')
time.sleep(7)
myExcel = app.denemeExcel.child_window(title="Smart View", control_type="TabItem").wrapper_object()
myExcel.click_input()
Panel = app.denemeExcel.child_window(title="Panel", control_type="Button").wrapper_object()
Panel.click_input()
time.sleep(1)
app.denemeExcel.print_control_identifiers()

Python - Can't go through "Open URL: xxx Always open this type of links in the associated application"

This is my first topic and question. I'm trying to write a script in Python(Pycharm) which help me log on to SAP via Citrix from the browser level. My problem is that I can't go through the window that pops out:
Open URL: xxx Always open this type of links in the associated application.
My script:
*imports that I need*
driver = webdriver.Chrome('C:\User\Chromium\chromedriver.exe')
driver.maximize_window()
driver.get('https://XYZ.dk')
Detect_Reciver = driver.find_element_by_xpath('//*[#id="protocolhandler-welcome"]/div/div/div/div/a')
if Detect_Reciver == Detect_Reciver:
Detect_Reciver.click()
time.sleep(2)
The problem appears here, after loading on a new page, the "Open URL: xxx Always open this type of links in the associated application." pops out, and I want to Press Enter or Escape by Python. Send_keys(keys.ENTER/ESCAPE) doesn't work.
enter image description here
Can you help me out :)?

Selenium python interact with FileOpen window

From Selenium, I managed to automate my tasks for a website.
However I ran into a problem:I need to upload a file to my web-page before submitting the form.
It is NOT an option to write the uploaded file into it's input element, because it is more complicated than this.
So basically I need to launch the FileUpload dialog by clicking a button, sendKeys there, and then close it by clicking on Ok.
I am wondering if this is even possible using just Selenium?
I am using it from python (so I don't have access to Robot class)
I tried so far:
element.click()
time.sleep(5)
alert = driver.switch_to.alert
alert.send_keys("path.to.myfile.txt")
alert.accept()
(nothing happens - I mean, the file open dialog works fine, but it does not send the keys )
I also tried:
alert = driver.switch_to.alert
buildu = ActionChains(driver).send_keys('path.to.my.file.txt')
buildu.perform()
(also not working)
Maybe I am looking at it wrong...
Maybe the alerts is not a good approach?
Do you have any idea?
I would prefere not having to use AUTOIT (for my own reasons)
So my goal is to click on a link element (DONE) then the link opens the File Upload open file dialog (DONE), then I need to be able to enter a text in the only textBox of the new window, and click on the Ok button
EDIT
This is the Open File dialog that shows up.
All I want to do is send the filename directly to the window (the textBox is focused when the dialog shows up, so no need to make more actions). After I send the keys (text) I need to be able to click on the Open button
You can create a Java program that will paste the filename and press the enter key. I had this same problem exactly. This is how I implemented:
package myaots_basic;
import java.io.*;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
public class trial {
public static void main(String[] args) throws AWTException {
System.out.println("HELLO WORLD");
StringSelection attach1 = new StringSelection ("C:\\My Office Documents\\Selinium projects\\Data\\attachment1.doc");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(attach1, null);
Robot rb1 = new Robot();
rb1.delay(3000);
rb1.keyPress(KeyEvent.VK_CONTROL);
rb1.keyPress(KeyEvent.VK_V);
rb1.keyRelease(KeyEvent.VK_V);
rb1.keyRelease(KeyEvent.VK_CONTROL);
rb1.delay(500);
rb1.keyPress(KeyEvent.VK_ENTER); // press Enter
rb1.keyRelease(KeyEvent.VK_ENTER);
}
}
Name it to trial.jar. Make this class a an executable.
Then in your python code just add a simple step:
import subprocess
subprocess.call("java -jar trial.jar", shell=True)
I don't know if this is restricted to windows 10 or works slightly different in other Windows versions but you can use the following code/idea.
Open the windows shell with win32com.client.Dispatch("WScript.Shell")
Send Tab keys to navigate through the open file dialog
Paste your absolute file path into the top of the dialog and also into the file selection text field and send enter key when
tab-navigating to the "open" button
Make a sleep of at least 1 second between each action.. otherwise Windows blocks this action.
import win32com.client as comclt
...
def handle_upload_file_dialog(self, file_path):
sleep = 1
windowsShell = comclt.Dispatch("WScript.Shell")
time.sleep(sleep)
windowsShell.SendKeys("{TAB}{TAB}{TAB}{TAB}{TAB}")
time.sleep(sleep)
windowsShell.SendKeys("{ENTER}")
time.sleep(sleep)
windowsShell.SendKeys(file_path)
time.sleep(sleep)
windowsShell.SendKeys("{TAB}{TAB}{TAB}{TAB}{TAB}")
time.sleep(sleep)
windowsShell.SendKeys(file_path)
time.sleep(sleep)
windowsShell.SendKeys("{TAB}{TAB}")
time.sleep(sleep)
windowsShell.SendKeys("{ENTER}")
...

python webdriver os window

I need to upload a file using Python and Selenium. When I click the upload HTML element a "File Upload" window is opened and the click() method does not return since it waits to fully load the page. Therefore I cannot continue using pywinauto code to control the window.
The first method clicks the HTML element (an img) to upload a new file:
def add_file(self):
return self.selenium.find_element(By.ID, "add_file").click()
and the second method is using pywinauto to type the path to the file and then click open
def upload(self):
from pywinauto import application
app = application.Application()
app.connect_(title_re = "File Upload")
app.file_upload.TypeKeys("C:\\Path\\To\\FIle")
app.file_upload.Open.Click()
How can I force add_file method to return and to be able to run the upload method?
Solve it. There was an iframe dealing with the upload but was hidden and didn't see it in the first place. The iframe contains an input of type file also hidden. To solve it make the iframe visible using javascript:
selenium.execute_script("document.getElementById('iframe_id').style.display = 'block';")
then switch to the iframe and make the input visible also:
selenium.switch_to_frame(0)
selenium.execute_script("document.getElementById('input_field_id').type = 'visible';")
and simply send the path to the input:
selenium.find_element(By.ID, 'input_field_id').send_keys("path\\\\to\\\\file")
For windows use 4 '\\\\' as path separator.

Categories