plam plam - 1 month ago 11
Python Question

Element not visible with pyvirtualdisplay and selenium in python

I'm trying a simple browser operation where I locate a username element on a website and then try to login. I'm using selenium and python to do this. Here's some simple code that works on my own local machine. The code opens a browser on my computer and then navigates to the correct username box and enters the username.

from selenium import webdriver
browser = webdriver.Firefox()
browser.get(my_url)
username_element = browser.find_elements_by_name("USERNAME")[0]
username_element.clear()
username_element.send_keys(my_username)


However, when I try to deploy the same code on an AWS server using pyvirtualdisplay so that Firefox doesn't need to pop up, it no longer works.

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))
display.start()

browser = webdriver.Firefox()
browser.get(my_url)
username_element = browser.find_elements_by_name("USERNAME")[0]
username_element.clear()
username_element.send_keys(my_username)


The element is definitely found, but I get the element not visible error:

selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with


which is confirmed by:

>> username_element.is_displayed()
False


I've tried various things I found on SO including:


  • making sure xvfb and xephyr are installed

  • adding a
    browser.implicitly_wait(30)

  • trying a
    WebDriverWait(browser,30).until(EC.visibility_of_element_located((By.NAME, "USERNAME")))
    which times out



Any ideas on how to solve this?

Answer

You can scroll screen:

browser.execute_script("window.scrollTo(0, 600)")