6

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 ?

PepperoniPizza
  • 8,842
  • 9
  • 58
  • 100

2 Answers2

13

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
Jon
  • 11,356
  • 5
  • 40
  • 74
  • If both versions fail - please add the exact url that I can test it directly. – Jon Dec 10 '13 at 16:53
  • 1
    Looks like installing `xserver-xephyr` did it, still the problem seems to be my box being to slow, I was able to deploy it to another box. Thanks ! – PepperoniPizza Dec 13 '13 at 14:53
1
from pyvirtualdisplay import Display 

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import Select

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

from selenium.common.exceptions import NoSuchElementException

from selenium.common.exceptions import NoAlertPresentException

from selenium.webdriver.common.keys import Keys

import unittest, time, re, random

capabilities = DesiredCapabilities.FIREFOX.copy()

capabilities['marionette'] = False

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

driver = webdriver.Firefox(capabilities=capabilities)

driver.implicitly_wait(20)

base_url = "http://xxx.yyy.zzz.aaa/sss/sss-Login/login/main_login.php"

RANDINT = random.random()*10000

verificationErrors = []

driver.get(base_url + "")

username = driver.find_element_by_id("myusername")

username.send_keys("xxxxxxxx")

driver.implicitly_wait(20)

password = driver.find_element_by_id("mypassword")

 #password.send_keys("xxxxzz" + Keys.ENTER)

password.send_keys("xxxxzzc" )

driver.implicitly_wait(20)

driver.find_element_by_xpath("//*[@id='submit']").click() 


 # Click on category link 


driver.find_element_by_xpath("//*[@id='stylefour']/ul/li[3]/a").click()

driver.find_element_by_xpath("//*[@id='stylefour']/ul/li[1]/a").click()

driver.find_element_by_xpath("//*[@id='stylefour']/ul[2]/li[4]/a").click

 # Click on sub-category link

driver.find_element_by_xpath("//*[@id='top']/body/div/div[2]/div[2]/div/div[2]/ul/li[4]/a/span").click()

 # Click on product image

driver.find_element_by_xpath("//*[@id='product-collection-image-374']").click()

 # Click Checkout button

driver.find_element_by_xpath("//*[@id='checkout-button']/span/span").click()

driver.find_element_by_id("billing:firstname").clear()

driver.find_element_by_id("billing:firstname").send_keys("selenium", RANDINT, "_fname")

driver.find_element_by_id("billing:lastname").clear()

driver.find_element_by_id("billing:lastname").send_keys("selenium", RANDINT, "_lname")

 # Click Place Order

driver.find_element_by_xpath("//*[@id='order_submit_button']").click()



driver.quit()

display.stop()
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • 2
    Welcome to Stack Overflow! While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Machavity Oct 12 '17 at 18:14