Saren Arterius
Saren Arterius

Reputation: 1340

Selenium Webdriver - PhantomJS hangs upon send_keys() to file input element

I am performing some file uploading tests. I found that my test code hangs at element.send_keys(file) if I am using PhantomJS, however the same code does not hang if I am using Firefox.

element = self.browser.find_element_by_xpath("//input[@type='file']")
element.send_keys(file)

Is there any workarounds to make PhantomJS upload files properly? Currently I am using Windows 7, Python 3.4.1, selenium 2.42.1, PhantomJS 1.9.7.

Upvotes: 6

Views: 2100

Answers (4)

I use this approach when I can not simply change the value of the file input tag.

We will run it console, so we should use PyVirtualDisplay with window manager (I am using dwm, you can try fluxbox it is easy to install but need more RAM then dwm) and Xephyr for debug. To run window manager we will use EasyProcess

So we will call File Upload Dialog, by clicking on element or button, then we simulate send keys with pynput and all these running in our console.

import time

from easyprocess import EasyProcess
from pynput.keyboard import Key, Controller
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


class ImageUploadAutomation:
    chrome = 'path/to/chrome'
    chrome_options = Options()
    # chrome_options.add_argument('--headless')
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-notifications")
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--window-size=1280,900")
    chrome_options.add_argument("--start-maximized")
    chrome_options.add_argument("user-data-dir=selenium")
    chrome_options.add_experimental_option(
        "excludeSwitches", ["disable-popup-blocking"]
    )
    chrome_options.add_argument(
        "--disable-blink-features=AutomationControlled"
    )

    driver = None

    def upload(self, photo):
        # Change visible to 1 if you want to use Xephyr debug
        with Display(visible=0, size=(1280, 900)) as display:
            with EasyProcess(["dwm"]) as process:
                keyboard = Controller()
                url = "https://www.exmaple.com"

                self.driver = webdriver.Chrome(
                    executable_path=self.chrome,
                    chrome_options=self.chrome_options
                )
                self.driver.get(url)

                test = self.driver.find_element_by_xpath(
                    "//div[@aria-label='Add Photos']"
                )
                time.sleep(1)
                test.click()

                time.sleep(1)

                for key in photo.path:
                    keyboard.press(key)
                    keyboard.release(key)

                # keyboard.press(Key.enter)
                with keyboard.pressed(Key.enter):
                    pass

Upvotes: 0

Lelouch
Lelouch

Reputation: 579

Oddly enough i couldn't get anything to run in my ubuntu shell but it would run via iPython from Jupyter notebook on the exact same server.

I had to add a virtual display into the code to make it run from the shell as a .py script...

if it helps anyone facing the similiar problem here is the lines of code i added to my script and the send keys start to work without an issue.

from pyvirtualdisplay import Display

# Set screen resolution to 1366 x 768 like most 15" laptops. This is needed 
#to run in the shell. Seems fine in iPython
display = Display(visible=0, size=(1366, 768))
display.start()

Upvotes: 0

SolessChong
SolessChong

Reputation: 3407

Should use PhantomJS.uploadFile(). However, didn't find python selenium API.

var webPage = require('webpage');   
var page = webPage.create();
page.uploadFile('input[name=image]', '/path/to/some/photo.jpg');

Upvotes: 0

M.A.K. Simanto
M.A.K. Simanto

Reputation: 692

browser = webdriver.PhantomJS()
browser.set_window_size(1200,800)

Without setting the window size, the browser remains in mobile size causing errors. Try a implicit wait too.

Upvotes: 1

Related Questions