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,460 points
1,715 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,320 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
• 58,090 points
23,381 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 21, 2018 in Selenium by king_kenny
• 3,710 points
12,525 views
0 votes
1 answer
0 votes
2 answers

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,500 points
12,576 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,558 views
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,910 points
6,603 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,506 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,320 points
1,994 views
0 votes
2 answers

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

String pageTitlte=driver.getTitle(); READ MORE

answered Sep 6, 2020 in Selenium by Sri
• 3,190 points
2,127 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP