1

My python selenium tests are working on firefox dirver (GUI) without any issue. But i wanted to run my tests in headless mode. When i try to run the same script with headless mode (with few modifications). it gives wierd errors.

Ex:

selenium.common.exceptions.NoSuchElementException: Message{"errorMessage":"Unable to find element with id 'ext-gen1499

python script :

import os
import time
from selenium.webdriver.common.proxy import *
from selenium.webdriver.common.by import By
phantomjs_path=r"/home/xxxx/nodejs-installs/phantomjs-2.1.1-linux-x86_64/bin/phantomjs"
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

service_args = ['--proxy=x.x.x.x:80','--proxy-type=https']
driver = webdriver.PhantomJS(executable_path=r'/home/xxxx/nodejs-installs/phantomjs-2.1.1-linux-x86_64/bin/phantomjs',service_args=service_args)

os.environ['MOZ_HEADLESS'] = '1'
driver.get("https://aaaaa.com")


def Login():
    try:
        driver.find_element_by_id("username").send_keys("[email protected]")
        driver.find_element_by_id("password").send_keys("xxxxxxx")
        driver.find_element_by_id("Submit").click()
        login_flag=1
    except:
        print("Error Loading Login page")
        login_flag=0
    finally:
        return login_flag

def CreateMail():
    try:
        element = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "button-1143-btnInnerEl")))
        driver.find_element_by_id("button-1143-btnInnerEl").click()

    except TimeoutException:
        print ("Loading took too much time - Create New Mail")

    driver.find_element_by_id("ext-gen1499").send_keys("[email protected]")
    driver.find_element_by_id("textfield-1265-inputEl").send_keys("Automated Test Mail from Selenium")
    driver.find_element_by_id("button-1252-btnIconEl").click()

Am i missing anything ?

3
  • 1
    I think the error says it all NoSuchElementException: with id 'ext-gen1499'. the id looks dynamic to me. Share the HTML please. Commented Nov 30, 2017 at 6:52
  • This is working with Firefox GUI. Same element-id Commented Nov 30, 2017 at 7:44
  • Share the HTML please Commented Nov 30, 2017 at 7:45

1 Answer 1

1

It is a good practice to add an implicit wait of at-least 10 seconds , for allowing the target page element/s to load completely.

driver.implicitly_wait(10)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.