Selenium with pyvirtualdisplay unable to locate element

0 votes

I have a working script that logs into a site using selenium like this:

script.py

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(1024, 768))
display.start()

browser = webdriver.Firefox()
actions = webdriver.ActionChains(browser)
browser.get('some_url_I_need')
content = browser.find_element_by_id('content') # Error on this line

running that script on an amazon ubuntu box through ssh where I installed firefox the following way: sudo apt-get install firefox

The error I get is:

selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element:{"method":"id","selector":"content"}'

If I run the same script on another ubuntu box through ssh too, it runs fine, no error, but I don't know how firefox was installed on that box, what could be the cause of that error. Is is related firefox installation and how to properly install it to be used with pyvirtualdisplay and selenium ?

Sep 14, 2018 in Selenium by Meci Matt
• 9,420 points
251 views

1 answer to this question.

0 votes

If there is some dynamic content on the website you need to wait some time until you can retrieve the wished element. Try to following code examples:

Check Configuration

  • Did you install a backend for pyvirtualdisplay like xvfb and xephyr? If not,

    try: sudo apt-get install xvfb xserver-xephyr

First try: Add a simple time.sleep()

import time
from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(1024, 768))
display.start()

browser = webdriver.Firefox()
actions = webdriver.ActionChains(browser)
browser.get('some_url_I_need')
time.sleep(5) # sleep for 5 seconds
content = browser.find_element_by_id('content') # Error on this line

Second try: Add browser.implicitly_wait(30) to your Selenium webdriver.

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(1024, 768))
display.start()

browser = webdriver.Firefox()
browser.implicitly_wait(30) # seconds
actions = webdriver.ActionChains(browser)
browser.get('some_url_I_need')
content = browser.find_element_by_id('content') # Error on this line
answered Sep 14, 2018 by Martin
• 4,220 points

Related Questions In Selenium

0 votes
2 answers

Unable to Click on an Element in Selenium (Python) even after finding it.

Here, I give you working script which ...READ MORE

answered Sep 19, 2018 in Selenium by Priyaj
• 56,520 points
7,395 views
0 votes
1 answer

Need to scroll down for locating an Element with Selenium

There are a couple of options for ...READ MORE

answered Apr 20, 2018 in Selenium by king_kenny
• 3,650 points
420 views
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
576 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
898 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
565 views
0 votes
1 answer

Basic authentication with Selenium in Internet Explorer 10

My upgrade to Internet Explorer 10 was ...READ MORE

answered Sep 14, 2018 in Selenium by Martin
• 4,220 points
402 views
0 votes
1 answer

How to get a page name using Selenium Webdriver (C#)?

I believe the below thing works: driver.Title; READ MORE

answered Sep 14, 2018 in Selenium by Martin
• 4,220 points
117 views