如何在Python中查找WebElement的常规xpath

时间:2019-04-12 17:17:55

标签: python selenium selenium-webdriver

我必须在网页中找到正确的登录表单(它必须在不同的网站上工作,所以我不能简单地使用id或类似的becausa显然可以在不同的网站上更改)。

我要做的是查找所有表单,而不是查找仅包含三个元素(用户,密码和访问按钮)的表单

user_field_list = ["username", "email", "user", "prova", "login_email", "session[username_or_email]"]
pass_field_list = ["password","Password", "pass", "login_password", "session[password]"]
button_field_list = ["Accedi", "login", "access", "Avanti"]
for form in browser.find_elements_by_xpath('.//form'):
   count=0
   find_user=0
   find_pass=0
   find_button=0
   for input_field in form.find_elements_by_xpath('.//input'):
      if (input_field.get_attribute("type") not in "hidden"):
         count=count+1
      if(find_pass == 0):
          for passkey in pass_field_list:
              if (passkey in input_field.get_attribute("type")):
                find_pass = 1
      if (find_user==0):
          for userkey in user_field_list:
             if (userkey in input_field.get_attribute("type")):
                find_user=1
      if (find_button==0):
          if ("submit" in input_field.get_attribute("type")):
              find_button=1

通过一些调试,我已经验证它找到正确的格式,但是在本部分的最后,我必须将对这三个字段的引用传递给另一个必须登录的函数

def general_login(browser, xpath_user, xpath_pass, xpath_button):
    print("general_login")
    try:
        print("System Log: SEARCH FOR USER FIELD ")
        print (str(xpath_user))
        username = browser.find_element_by_xpath(xpath_user)
        print("System Log: FOUND USER FIELD")
        username.send_keys(usernameStr)
        print("System Log: SEARCH FOR PASSWORD FIELD")
        password = browser.find_element_by_xpath(xpath_pass)
        print("System Log: FOUND PASSWORD FIELD")
        password.send_keys(passwordStr)
        print("System Log: SEARCH FOR BUTTON ACCEDI")
        accediButton = browser.find_element_by_xpath(xpath_button)
        print("System Log: FOUND BUTTON ACCEDI")
        accediButton.click()
        return 0
    except:
        return 1

问题是我不知道如何从webElements中检索三个通用的xpath(而且我对如何将正确的引用传递给函数没有其他选择)。

有人可以帮助我如何检索xpath或我可以使用的其他可能性?谢谢!

1 个答案:

答案 0 :(得分:0)

您可以将元素传递到general_login,而不必尝试从WebElement获取xpath

get_form_fields方法下面,返回空或带有表单字段字典。如果字典为空,general_login将失败:

def get_form_fields(browser, user_field_types, password_field_types, button_types):
    # get form fields as dictionary with keys: user_field, password_field, submit_button

    form_fields = {}
    for form in browser.find_elements_by_xpath('.//form'):

        inputs = list(filter(lambda x: x.is_displayed(), form.find_elements_by_xpath('.//input')))
        if len(inputs) < 3:
            continue

        user_field_list = list(filter(lambda x: x.get_attribute("type") in user_field_types, inputs))
        if len(user_field_list) != 1:
            form_fields.clear()
            continue
        else:
            form_fields["user_field"] = user_field_list[0]

        password_field_list = list(filter(lambda x: x.get_attribute("type") in password_field_types, inputs))
        if len(password_field_list) != 1:
            form_fields.clear()
            continue
        else:
            form_fields["password_field"] = password_field_list[0]

        button_list = list(filter(lambda x: x.get_attribute("type") in button_types, inputs))
        if len(button_list) != 1:
            form_fields.clear()
            continue
        else:
            form_fields["submit_button"] = button_list[0]

    return form_fields

general_login使用:

def general_login():
    user_field_types = ["username", "email", "user", "prova", "login_email", "session[username_or_email]"]
    password_field_types = ["password", "Password", "pass", "login_password", "session[password]"]
    button_types = ["Accedi", "login", "access", "Avanti"]
    form_fields = get_form_fields(browser, user_field_types, password_field_types, button_types)

    assert form_fields

    print("general_login")
    print("System Log: SEARCH FOR USER FIELD ")
    form_fields["user_field"].send_keys(username)

    print("System Log: SEARCH FOR PASSWORD FIELD")
    form_fields["password_field"].send_keys(password)

    print("System Log: SEARCH FOR BUTTON ACCEDI")
    form_fields["submit_button"].click()


browser = webdriver.Chrome()

username = "user"
password = "password"

general_login()