Selenium Dynamic Element Python

前端 未结 3 1590
北恋
北恋 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:

    
     
      

    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.

提交回复
热议问题