Selenium WebDriver Core API & Commands 2 — Questions and Answers
Question 1: Which method sends keyboard input to a WebElement?
- element.sendKeys() (Correct answer)
- element.type()
- element.input()
- element.write()
Correct answer: element.sendKeys()
sendKeys() simulates typing text or special key presses into a focused web element.
Question 2: How do you clear the text in an input field using WebDriver?
- element.clear() (Correct answer)
- element.reset()
- element.empty()
- element.flush()
Correct answer: element.clear()
element.clear() removes all content from an editable element such as a text box.
Question 3: What does driver.getCurrentUrl() return?
- The URL of the currently loaded page (Correct answer)
- The base URL set during setup
- The last visited URL
- The canonical URL from the page meta tag
Correct answer: The URL of the currently loaded page
driver.getCurrentUrl() returns the full URL string of the page currently displayed in the browser.
Question 4: Which method retrieves the visible text of a WebElement?
- element.getText() (Correct answer)
- element.getValue()
- element.getContent()
- element.getInnerText()
Correct answer: element.getText()
element.getText() returns the visible, rendered text of the element as a String.
Question 5: How do you get the value of an HTML attribute on a WebElement?
- element.getAttribute('name') (Correct answer)
- element.getProperty('name')
- element.getAttr('name')
- element.readAttribute('name')
Correct answer: element.getAttribute('name')
getAttribute() returns the value of the specified HTML attribute for the element.
Question 6: Which method checks if a WebElement is currently displayed on the page?
- element.isDisplayed() (Correct answer)
- element.isVisible()
- element.isShown()
- element.isRendered()
Correct answer: element.isDisplayed()
isDisplayed() returns true only if the element is visible to the user in the current viewport.
Which method sends keyboard input to a WebElement?