Question about using selenium+pyautogui to upload file - python

Recently I need to upload my dataset on a website, the first part works fine on selenium.
However, after I click the 'upload' button, a windows file broswer pops-up.
And after searching online, I finally make it work by using pyautogui, code:
pyautogui.write('K:\Github\###\data\global\Global_PM_corT.csv') # enter file with path
pyautogui.press('enter') # click ok
My question is, I want to use above process on Github Action.
So, i changed:
'K:\Github\###\data\global\Global_PM_corT.csv'
to:
'./data/global/Global_PM_corT.csv'
and it's not working.
I think there is some error when using relative path with pyautogui.
So, i was wondering if someone could help me achieve this? Thanks in advance:)
P.S.
its working right now.
I first read the dataset by
pd.read_csv('path')
then write it on github action
to_csv('C:\\Global_PM_corT.csv')
then I can use absolute path with pyautogui:)
However, I still want to know if a better way to achieve this

Please try this:
'./data/global/Global_PM_corT.csv'

Related

Is there a cross-platform solution to interacting with node OS when using selenium grid?

I wrote some selenium tests that were run and passed locally. I wrote them for Windows/Chrome. At one point, my app opens up a local file explorer to upload a file. To interact with this I used AutoIt, which solved my problem. However, now I am moving my tests to grid (through lambdatest) and I am testing on both Windows and Mac. AutoIt only works on Windows, and out of the box it only works locally. I know some solutions have been offered to use AutoIt remotely, but this doesn't help me with Mac OS.
Is there a general solution for how to approach interacting with a remote machine during a selenium test?
Any help is appreciated. I'm not sure how to approach this.
EDIT:
After looking around at a lot of solutions to this, the below is what I have been trying but the test times out because it cannot find the file input. I know there must be some way to do this. People testing apps on MacOS can't use AutoIT, so what is the solution?
# upload photo
photo_upload_element = wait1.until(
EC.visibility_of_element_located((By.CLASS_NAME, 'reply-field__button--cam'))).click()
time.sleep(2)
file_input = self.driver1.find_element_by_css_selector("input[type='file']")
time.sleep(2)
file_input.send_keys(DIR_PATH + r"\path\to\local\photo.jpg")
time.sleep(2)
file_input.send_keys(Keys.RETURN)
time.sleep(2)
wait1.until(
EC.visibility_of_element_located((By.CLASS_NAME, 'buttons-grid__button'))).click()
time.sleep(2)
Here is the html on the page for the input:
<template>
<input
class="fileselect"
v-on:change="handleFileSelect"
ref="fileinput"
id="cam"
type="file"
accept="image/*"
:capture="camera"
data-testid="fileselect"
/>
</template>
Can anyone suggest a solution?
Please make sure you use the file detector is implemented when running tests on the grid that should take care of this kind of issue.
You can refer to the below link for more information on the file detector.
File Detector in Remoter Driver implementation
I think we don't have to use AutoIT in this case, as the input type is file ideally send_keys method should take care of browsing the file.
I think you are trying to manage the browse file issue. If so, you can use pyautogui:
from pyautogui import typewrite
typewrite('file.txt')
typewrite(['enter'])

How to disable/enable "Save as" dialog in Selenium Chromedriver?

I googled this question and found a lot of answers, still non of the solutions worked for me.
So my question is: how to Enable "Save as" dialog for Selenium Chromedriver? I want to see this dialog when I download a file.
Good to see how to do it in Python but any other languages are good too.
I've tried to add the next line "{download.prompt_for_download': 'true'}" to arguments and to experimental options:
chrome_options.add_experimental_option("prefs"{'download.prompt_for_download': 'true'})
chrome_options.add_argument({'download.prompt_for_download': 'true'})
The first one didn't give any result, the second one caused code error.
I also tried "saveas-menu-label" and got no result.
If you want to just see the dialog when you download the file, open settings and select "Ask where to save each file before downloading".
If your configuration requires to do this from the code, try this solution:
"How to make the settings for Download in Chrome when launched using Chromedriver?"
There is a syntax error in your code as true/false are supposed to be given in boolean and not string. So in python, it could be so
chrome_options.add_experimental_option("prefs",{'download.prompt_for_download': True})
Hope this helps you out.

How to click on a video using Selenium

I'm new to coding using python and it's libraries, and also new to stackoverflow so I apologize if I'm not acquainted to some things.
Anyway to the question. I'm trying to write code that automates playing a video from the website that. Here's an example link:
http://www.shush.se/index.php?id=164&show=southpark
I've tried these methods:
driver.find_element_by_id("playerload").click()
driver.find_elements_by_xpath("./html/body/table/tbody/tr/td/div[#id='load']/div[#class='player']/div[#id='playerload']/div[1]")
But nothing happens. The program exits without error but the video does not
start playing.
I tried clicking a the id "jw6playerid5040619_wrapper" but the number associated with that changes every time a page loads.
Any advice? Thanks in advance!
Even though you don't know the exact id, you know that it will start with "jw6playerid". The following works for me:
elements = driver.find_elements_by_xpath("//*[contains(#id, 'jw6playerid')]")
elements[0].click()

How can I run a python script from within Kodi?

If I want to make a button in Kodi's menu and run a local python script upon clicking it, what's the best way to go about it?
file_path = xbmc.translatePath(os.path.join('insert path here to file you want to run'))
xbmc.executebuiltin("XBMC.RunScript("+file_path+")")
Very late reply but saw no one else had answered so though i'd put in just in case

Python selenium: Firefox auto download everything

I am trying to download a csv file from a website and no matter how many MIME's I try for both saving and opening, the dialog box still appears. Is there a way to cover all MIME's at once or anything that might pop up?
Right now I'm using:
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/html")
fp.set_preference("browser.helperApps.neverAsk.openFile","text/html")
And other variations including a ton more MIME's. How can I auto download regardless of the MIME?
I solved my question with this:
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream")
fp.set_preference("browser.helperApps.neverAsk.openFile","application/octet-stream")
I had multiple preference statements, and I think that is what the problem was. Now I only have this one active and don't have any problems. Hope this can help someone else.

Categories