The document provides a comprehensive guide on performing operations on web elements using Selenium Web Driver, including text boxes, radio buttons, check boxes, dropdowns, links, and XPath. It details methods for interacting with these elements, such as typing text, retrieving values, selecting options, and clicking links. Additionally, it includes examples of XPath syntax for locating elements based on various attributes and conditions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
6 views3 pages
Operations On Web Elements
The document provides a comprehensive guide on performing operations on web elements using Selenium Web Driver, including text boxes, radio buttons, check boxes, dropdowns, links, and XPath. It details methods for interacting with these elements, such as typing text, retrieving values, selecting options, and clicking links. Additionally, it includes examples of XPath syntax for locating elements based on various attributes and conditions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3
2.
Operations on Web Elements
Text Box
1. How to Type in text box using Selenium Web Driver
WebElement textBox = driver.findElement(By.id("textbox_id")); textBox.sendKeys("Sample Text"); 2. How to get a Text Box value using Selenium Web Driver String value = driver.findElement(By.id("textbox_id")).getAttribute("value"); System.out.println("Text Box Value: " + value); 3. How to read the placeholder value of a Text Box using getAttribute() method String placeholder = driver.findElement(By.id("textbox_id")).getAttribute("placeholder"); System.out.println("Placeholder Value: " + placeholder); 4. Deleting/clear text from the Text Box driver.findElement(By.id("textbox_id")).clear(); 5. Check if Text Box is enabled/disabled boolean isEnabled = driver.findElement(By.id("textbox_id")).isEnabled(); System.out.println("Text Box Enabled: " + isEnabled);
Radio Button / Check Box
1. Selecting the Radio Button by Label Text / value
driver.findElement(By.xpath("//input[@value='option_value']")).click(); 2. Find out number of elements in a radio group List<WebElement> radioButtons = driver.findElements(By.name("radio_group")); System.out.println("Number of Radio Buttons: " + radioButtons.size()); 3. Find out all radio button values for (WebElement radio : radioButtons) { System.out.println("Radio Button Value: " + radio.getAttribute("value")); } 4. How to get the selected radio button label text? for (WebElement radio : radioButtons) { if (radio.isSelected()) { System.out.println("Selected Radio Button: " + radio.getAttribute("value")); break; } } 5. Check if Radio Button is selected? boolean isSelected = driver.findElement(By.id("radio_id")).isSelected(); System.out.println("Radio Button Selected: " + isSelected); 6. Check if Radio Button is enabled or disabled? boolean isEnabled = driver.findElement(By.id("radio_id")).isEnabled(); System.out.println("Radio Button Enabled: " + isEnabled);
Dropdown/List Box/Combo Box
1. Print all the options available in the dropdown
Select dropdown = new Select(driver.findElement(By.id("dropdown_id"))); List<WebElement> options = dropdown.getOptions(); for (WebElement option : options) { System.out.println("Dropdown Option: " + option.getText()); } 2. Print first selected option from a dropdown System.out.println("First Selected Option: " + dropdown.getFirstSelectedOption().getText()); 3. Select an option by value from a dropdown dropdown.selectByValue("option_value"); 4. Select an option by visible text from a dropdown dropdown.selectByVisibleText("Option Text"); 5. Select an option by index from a dropdown
dropdown.selectByIndex(2); // Selects the 3rd option (0-based index)
Link
1. Clicking a link using partialLinkText
driver.findElement(By.partialLinkText("Partial Link Text")).click(); 2. Clicking a link using link Text driver.findElement(By.linkText("Full Link Text")).click(); 3. Find out all the links in a web page List<WebElement> links = driver.findElements(By.tagName("a")); for (WebElement link : links) { System.out.println("Link: " + link.getText() + " - " + link.getAttribute("href")); } 4. Clicking on an image link driver.findElement(By.xpath("//img[@alt='image_alt_text']")).click();
XPath
1. Read label text and its color
WebElement label = driver.findElement(By.id("label_id")); String text = label.getText(); String color = label.getCssValue("color"); System.out.println("Label Text: " + text); System.out.println("Label Color: " + color); 2. Write an xpath for id, name, className //*[@id='element_id'] //*[@name='element_name'] //*[@class='element_class'] 3. How to find an element that contains a specific text using contains() //button[contains(text(), 'Submit')] 4. How to find an element using text() //h1[text()='Welcome'] 5. How to find an element that starts-with() //input[starts-with(@id, 'user')] 6. XPath to select following-sibling and preceding //label[@id='label_id']/following-sibling::input //input[@id='input_id']/preceding::label 7. XPath to select an element using ancestor, child, parent and descendent //div[@id='parent_id']/child::input //input[@id='child_id']/parent::div //div[@id='ancestor_id']/descendant::input //input[@id='descendant_id']/ancestor::div 8. XPath using OR and AND //input[@type='text' or @type='password'] // Matches either type //input[@type='text' and @name='username'] // Matches both conditions