Upload file using Selenium webdriver. Is there any crossbrowser solution? - python

Hi I am trying to figure out how to upload image by Selenium webdriver in python. Here is my code:
element = driver.find_element_by_id("fileUpload")
element.send_keys("C:\myfile.jpg")
It works well in Chrome, but not in other browsers. The problem is that it either opens OS native file browser or do nothing.
Is there any solution that works in all browsers? (I need Chrome, Firefox, IE, Edge)

Related

Browser not opening using Selenium and Kora on Google Colab

The browser will not open with the "get" object while using Google Colab with Selenium and Kora. Here is my code:
from kora.selenium import wd
website = "https://www.espn.com"
wd.get(website)
It just returns "completed" and nothing happens. Any advice?
By the browser won't open, you mean that you are expecting a window to open?
Because if so, that's just not possible. It runs headless. You can't redirect the server's - that colab is hosted on - monitor image to yours.

Upload file by sendkeys in python opens a file selector popup

The component which needs to be automated is antd - upload.
https://ant.design/components/upload/ --> Can be found here
The button for the upload is visible, but the input "tag" is not visible:
Component view: This is the button how it is seen on the screen
HTML view: This is the view of the inspection of the component
As you see the "input" is not visible.
To have selenium interact with the input (to use the sendkeys) I need to have the input element visible on the screen.
I make an execute script as follows:
file_input = self.driver.find_element_by_xpath(xpath)
self.driver.execute_script('arguments[0].style.display = "block"; ', file_input)
After this I get the following view of the component: component ui view
And the html view: HTML view
After this I interact with the component by sending keys to the element:
file_input.send_keys(fpath)
At this step I have 2 things happening:
1. the file selector popup is shown
2. the file is uploaded by the send_keys.
This causes an issue when I try to run the scripts in headless browser. The message that is shown:
SessionNotCreatedException: Message: Tried to run command without establishing a connection headless browser
This is only due to running in headless browser mode. When the browser is displayed, the scripts are kept going. I have also tried using on both displayed and headless browser the module pyautogui:
pyautogui.keyDown('esc')
pyautogui.keyUp('esc')
This helps only on the displayed browser, so the popup is closed. But for headless browser this doesn't help.
I am running the scripts on MacOS Sierra, Firefox (58.0.2) in headless browser options, python 2.7, selenium 3.8
Would be very grateful if someone knows how to work around this.
Thank you
Update with the latest chromedriver and chrome. This is working without having a popup come out for headless browser when running this step by send-keys in selenium.
Firefox still has this issue with geckodriver.

Use existing open tab and url in Selenium py

Hi I'm trying to use Selenium python to use a url that is already open on Internet Explorer. I had a look around and not sure if this is possible.
The reason why I wouldn't like to open new brower or tab is because the page changes to different text.
So far my text only opens a new browser
CODE
from selenium import webdriver
driver = webdriver.Ie()
driver.get("https://outlook.live.com/owa/")
This answer helped me with same problem.
By now you can not access previously opened tabs with selenium.
But you can try to recreate your session, passing what is needed using requests library, for example.

Having no style in Chrome Webdriver (Selenium) as Firefox menu built function

I'm recently using selenium chrome webdriver to scrape some data of simple websites, now I'm targeting to lower the time it takes to open the page. I have found the list of parameters I can turn off in Chrome - link1, now I'm wondering what are the functions Mozilla Firefox automatically turns off when I simple click "no style" on the browser menu.
I sucessfully turned off images, but I think there are other things to disable.
prefs = {"profile.managed_default_content_settings.images":1,}
link1 : http://peter.sh/experiments/chromium-command-line-switches/#disable-application-cache
*** Edit: I have already tried PhantomJS, I have tested it and apearently it took more time to load pages than chrome.

Keys.ESCAPE in selenium webdriver(python)

I am using selenium webdriver (with python). I have a use case where I want to test that submit button get disabled once the form get submited. To test it, I send ESCAPE key to stop the page to load next page so that I can access the elements of same page.
password.send_keys("abcdef", Keys.ENTER, Keys.ESCAPE)
The problem is that it works fine in Firefox browser but it is not working in Chrome. In Chrome sending ESCAPE seems to be not working and it submits the form and loads the next page.
Is there any other solution or workaround to overcome this?
After trying many options, finally the following option seems to be working -
password.send_keys("abcdef", Keys.ENTER, Keys.ESCAPE) # this works for Firefox driver
drive.execute_script("window.stop();") # this works for Chrome driver
You can try one of these 3 suggestions from https://sqa.stackexchange.com/a/6208 :
Installing an extension in Chrome called Stop load
using the built in pageLoadTimeout() method in Selenium
using Sikuli or Autoit to access the browser's native controls

Categories

Resources