Python 莴苣/硒删除新创建的DOM元素

Python 莴苣/硒删除新创建的DOM元素,python,selenium,selenium-webdriver,bdd,lettuce,Python,Selenium,Selenium Webdriver,Bdd,Lettuce,场景 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(u'I click

场景

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(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)
错误

NoSuchElementException:消息:u'没有此类元素(会话信息: 铬=31.0.1650.63)(驾驶员信息: chromedriver=2.8.240825,平台=Linux 3.8.0-34-通用x86_64)'

说明

单击“创建”按钮时,会在DOM中插入一个div元素(通过JavaScript),显示“First name”和“Last name”字段。在窗口可见的情况下运行时,我可以看到div元素在消失之前短暂出现,并导致“I fill…”步骤失败

从我所读到的内容来看,关注新创建的div元素是一个问题吗

尝试的解决方案

  • ActionChains(world.browser)。将\移动到\元素(按钮)。单击()。执行()
  • world.browser.switchTo()和朋友
  • world.browser.find_元素(…).send_键(“\n”)
  • 睡眠直到,含蓄地等待,等等
环境

  • PyVirtualDisplay 0.1.2
  • 硒2.38.4
  • 莴苣0.2.19
  • Python 2.7.4
  • Lubuntu Linux 13.10
研究


我不知道为什么这样可以解决问题,但它确实解决了问题

@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)
在“单击”步骤之后,我立即调用了下一步,但浏览器可能还并没有呈现新元素。莴苣和硒可能比浏览器工作得更快。