Get All Elements In A Form April 21, 2024 Post a Comment I would like to use Selenium to submit a form which contains several elements. For example: UsernameSolution 1: You can use xpath to get all direct child elements of a specific element using parent/*.If you already have your form element using findElement(), as below:WebElement formElement = driver.findElement(By.name("something")); List<WebElement> allFormChildElements = formElement.findElements(By.xpath("*")); Copyor directly using: List<WebElement> allFormChildElements = driver.findElements(By.xpath("//form[@name='something']/*")); CopyThen look at the tag and type of each element to specify its value:for (WebElement item : allFormChildElements) { if (item.getTagName().equals("input")) { switch (item.getAttribute("type")) { case"text": //specify text valuebreak; case"checkbox": //check or uncheckbreak; //and so on } } elseif (item.getTagName().equals("select")) { //select an item from the select list } } CopySolution 2: driver = webdriver.Firefox() driver.get("https://www.hackerearth.com/problems/") #find all form input fields via form name _inputs = driver.find_elements_by_xpath('//form[@name="signup-form"]//input') forinputin _inputs: #print attribute name of each input element printinput.get_attribute('name') Copyo/p first_name last_name email password submitSolution 3: Sorry, missed the point of your question first. You can locate any element witihin the form with xpath locators, for example. In your caseBaca JugaCross Browser Issue : Document.getelementbyid().value Not Working In Ie But Works In FirefoxAndroid Webview Memory Leak When Using AssetsHow Can You Flip Website Upside Down In Ie ? (for The April 1st)find.Element(By.xpath("//form/*[@name='a']")) find.Element(By.xpath("//form/*[@name='b']")) find.Element(By.xpath("//form/*[@name='c']")) CopyIf you have multiple form tags on your page, you can specify it with any attribute as well. find.Element(By.xpath("//form[@name='something']/*[@name='c']")) //as it is in your sample CopyAlso you can specify form first, and work with elements within it. I'm not sure abut your syntax, but first, you need to return the form webelement into some var (let it be form) in any way. After that you may pass this var instead of webdriver instance.form.find.Element(By.xpath('./some/child/locator')) CopySolution 4: Store the form element in a variable, then use it as the search context to find the child elements: WebElementformElement= driver.findElement(By.name("something")); WebElementa= formElement.findElement(By.name("a")); WebElementb= formElement.findElement(By.name("b")); WebElementc= formElement.findElement(By.name("c")); a.sendKeys("first child element [a]"); b.sendKeys("password"); c.submit(); Copy Share You may like these postsAdjust Height On Jqueryui Dialog To Be As Tall As Content Up To A MaxGet Parent Page To Scroll To Top When Iframe's Form Validation FailsUnique Url For Article ViewerShow And Hide Active Class In Li When Clicked On Input Button Post a Comment for "Get All Elements In A Form"
Post a Comment for "Get All Elements In A Form"