Question

Scenario

Given I navigate to the table menu
When I click the "Add" button
And I fill "First name" with "Bob"
And I fill "Last name" with "Dylan"
And I click the "Create" button
Then I should see a new entry with a name of "Bob Dylan"

Step Definitions

@step(u'I click the "(.*)" button')
def i_click_the_named_button(step, field):
    button = world.browser.find_element_by_xpath('(//a[contains(text(),"{0}") and not(contains(@style, "display:none"))] | //input[@value="{0}" and not(contains(@style, "display:none"))] | //li[contains(text(), "{0}") and not(contains(@style, "display:none"))])[1]'.format(field))
    button.click()

@step(u'I fill "(.*)" with "(.*)"')
def i_fill_in_field_with_value(step, field, value):
    try:
        label = world.browser.find_element_by_xpath('//label[contains(text(),"%s")]' % field)
        id = label.get_attribute('for')

        input = world.browser.find_element_by_id(id)
        input.clear()
        input.send_keys(value)
    except:
        input = world.browser.find_element_by_xpath('//input[@placeholder="%s"]' % field)    
        input.clear()
        input.send_keys(value)

Error

NoSuchElementException: Message: u' no such element (Session info: chrome=31.0.1650.63) (Driver info: chromedriver=2.8.240825,platform=Linux 3.8.0-34-generic x86_64) '

Description

When clicking the 'Create' button a div element is inserted into the DOM (via JavaScript) displaying the 'First name' and 'Last name' fields. When running with a window visible I can see the div element appear briefly before vanishing and causing the 'I fill...' steps to fail.

From what I've read its an issue with maintaining focus on the newly created div element?

Solutions Attempted

  • ActionChains(world.browser).move_to_element(button).click().perform()
  • world.browser.switchTo() and friends
  • world.browser.find_element(...).send_keys("\n")
  • sleep_until, implicitly_wait, etc...

Environment

  • PyVirtualDisplay 0.1.2
  • Selenium 2.38.4
  • Lettuce 0.2.19
  • Python 2.7.4
  • Lubuntu Linux 13.10

Research

Keeping the focus after opening a new window in the Selenium IDE

Using Python bindings, Selenium WebDriver click() is not working sometimes.

How to switch to the new browser window, which opens after click on the button?

Selenium WebDriver ActionChains

WebDriver.py implementation

Selenium API

Was it helpful?

Solution

I know not why this fixes the problem, but it has indeed resolved the situation.

@step(u'I click the "(.*)" button')
def i_click_the_named_button(step, field):
    button = world.browser.find_element_by_xpath('(//a[contains(text(),"{0}") and not(contains(@style, "display:none"))] | //input[@value="{0}" and not(contains(@style, "display:none"))] | //li[contains(text(), "{0}") and not(contains(@style, "display:none"))])[1]'.format(field))
    button.click()
    time.sleep(1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top