Selenium Dynamic Element Python

前端 未结 3 1597
北恋
北恋 2021-01-29 00:12

Hello I am using selenium. I have to send key to this input.

相关标签:
3条回答
  • 2021-01-29 01:07

    you can find them by xpath

    i.e:

    <html>
     <body>
      <form id="loginForm">
    </body>
    <html>
    

    you can get by:

    login_form = driver.find_element_by_xpath("/html/body/form[1]")
    

    the number 1 here indicates that its the first form. in your case if you know the form you can use the following(just change the number to match yours. i.e if its the 4th input then change the value to 4)

    driver.find_element_by_xpath("//form[1]/input[1]")
    

    also another alternative is in cases where name, type and some other attributes don't change you can use(chaining them so they point to a unique element):

    driver.find_element_by_xpath("//input[@name='emailAddress'][@type='email']")
    

    to validate if the xpath will work, try the search box in the web inspector, it accept xpath and if it finds your element, then it will work in python too.

    refer to https://selenium-python.readthedocs.io/locating-elements.html for more ways.

    0 讨论(0)
  • 2021-01-29 01:08

    You Can Locate element using xpath kr css where id or classname is not unique.

    driver.find_element_by_xpath("//input[@name='emailAddress']")
    

    Or

    driver.find_element_by_name('emailAddress')
    

    Or

    driver.find_element_by_css_selector("input[name='emailAddress']")
    

    Note: you can do chaining aswell if combination of attributes are unique:

    driver.find_element_by_xpath("//input[@name='emailAddress'][@type='email']")
    
    0 讨论(0)
  • 2021-01-29 01:09

    You use any of the unique selectors for the input field: type="email" placeholder="E-posta adresi" value="" name="emailAddress" data-componentname="emailAddress"

    xpath:

    driver.find_element_by_xpath("//input[@name='emailAddress' and contains(@placeholder, 'E-posta adresi']")
    

    css:

    driver.find_element_by_css_selector("input[name='emailAddress'][type='email']")
    
    0 讨论(0)
提交回复
热议问题