This article introduces how we use selenium to manipulate various page elements
Reading Contents
Link (link)
<div> <p>Link link</p> <a href="www.cnblogs.com/tankxiao">Little Tank</a> </div>
Link operation
// Find the link element WebElement link1 = driver.findElement(By.linkText("small tank")); WebElement link11 = driver.findElement(By.partialLinkText("tank")); // Click the link link1.click();Input box textbox
<div> <p>Input box testbox</p> <input type="text" id="usernameid" value="username" /> </div>
Operation of input box
// Find the element WebElement element = driver.findElement(By.id("usernameid")); // Enter content element.sendKeys("test111111"); // Clear input box element.clear(); // Get the content of the input box element.getAttribute("value");Button
<div> <p>Button button</p> <input type="button" value="Add" id="proAddItem_0" /> </div>
Find the button element
//Find the button element String xpath="//input[@value='Add']"; WebElement addButton = driver.findElement(By.xpath(xpath)); // Click the button addButton.click(); // Determine whether the button enables addButton.isEnabled();
Pull down selection box (Select)
<div> <p>Drop-down selection box Select</p> <select id="proAddItem_kind" name="kind"> <option value="1">Computer hardware</option> <option value="2">Real Estate</option> <option value="18">Category AA</option> <option value="19">Category BB</option> <option value="20">Category BB</option> <option value="21">Category CC</option> </select> </div>
The operation of drop-down selection box
// Find the element Select select = new Select(driver.findElement(By.id("proAddItem_kind"))); // Select the corresponding selection, index starting from 0 select.selectByIndex(2); select.selectByValue("18"); select.selectByVisibleText("Category AA"); // Get all options List<WebElement> options = select.getOptions(); for (WebElement webElement : options) { System.out.println(webElement.getText()); }Radio Button
<div> <p>Single-option Radio Button</p> <input type="radio" value="Apple" name="fruit>" />Apple <input type="radio" value="Pear" name="fruit>" />Pear <input type="radio" value="Banana" name="fruit>" />Banana <input type="radio" value="Orange" name="fruit>" />Orange </div>
Operation of single-option elements
// Find the radio box element String xpath="//input[@type='radio'][@value='Apple']"; WebElement apple = driver.findElement(By.xpath(xpath)); //Select a radio box apple.click(); //Judge whether a radio box has been selected boolean isAppleSelect = apple.isSelected(); //Get the element attribute apple.getAttribute("value");Multiple check box
<div> <p>Multi-option checkbox</p> <input type="checkbox" value="Apple" name="fruit>" />Apple <input type="checkbox" value="Pear" name="fruit>" />Pear <input type="checkbox" value="Banana" name="fruit>" />Banana <input type="checkbox" value="Orange" name="fruit>" />Orange </div>
The operation of multi-check boxes is exactly the same as that of a single-check box, so I won’t talk about it here.
The above is the information on common web UI element operations of java selenium. We will continue to add it later. Thank you for your support for this site!