我正在使用Python语言中的Behave框架,我以前从未使用过它,我不确定如何才能点击element_by_id。有一个cookie弹出窗口,我需要绕过它,然后才能发送登录密钥。
这是我的.features文件:
Feature:
Login Functionality
Scenario: I can login
When visit url "https://example.com"
When I click on the button "accept-cookie-notification"
When field with name "user_email_login" is given "@gmail.com"
When field with name "user_password" is given "password"
Then title becomes "Dashboard"这是我的.py文件:
步骤
@when('visit url "{url}"')
def step(context, url):
context.browser.get(url)
time.sleep(5)
@when('I click on the button "{selector}"')
def step(context, selector,):
elem = context.driver.find_element_by_id("selector")
elem.submit()
time.sleep(5)
@when('field with name "{selector}" is given "{value}"')
def step(context, selector, value):
elem = context.browser.find_element_by_id(selector)
elem.send_keys(value)
elem.submit()
time.sleep(5)
@then('title becomes "{title}"')
def step(context, title):
assert context.browser.title == title另外,我稍后还需要做element_by_css和xpath。
提前感谢您的帮助。
发布于 2019-10-15 19:34:28
如果您使用的是Behave,那么使用Selenium是非常简单的。只需安装behave webdriver package https://pypi.org/project/behave-webdriver/它是一个步骤库,包含一系列已经定义的步骤,带有给定的-当-然后装饰符,比如:I click on the element "{element}",你可以使用id,css和XPath作为{element}参数。您不需要实现任何东西,只需在您的场景中使用预定义的步骤。下面是我的代码示例:
Scenario: A user can log in.
Given the base url is "http://my_site:3000"
And I open the site "/#/login"
And the element "#email" is visible
When I add "my@email.com" to the inputfield "#email"
And I add "1234567" to the inputfield "#password"
And I click on the button "#loginButton"
Then I wait on element "//nav-toolbar//span[contains(text(),'Myself')]" to be visible
And I expect that the attribute "label" from element "#loggedUser" is "Finally, we made it!"请不要忘记将处理context.behave_drive添加到您的environment.py方法before_all()和after_all()中,如上面的HOWTO页面所述。
发布于 2019-09-23 18:37:10
你需要的是阅读一些文档,试着看看你在哪里有最常用的命令http://allselenium.info/python-selenium-commands-cheat-sheet-frequently-used/
https://stackoverflow.com/questions/58033697
复制相似问题