4. Identify the error in this Selenium Java code snippet:
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.id("drag"));
new Actions(driver).clickAndHold(element);
actions.perform();
medium
A. Incorrect locator used for element
B. Missing chaining of clickAndHold() with perform()
C. perform() should be called on driver, not actions
D. clickAndHold() cannot be used without release()
Solution
Step 1: Check method chaining
The clickAndHold(element) returns an Actions object and should be chained with perform() to execute immediately.
Step 2: Analyze the code calls
Here, clickAndHold(element) is called but not chained; then actions.perform() is called separately, which performs no action because the previous call was not chained.
Final Answer:
Missing chaining of clickAndHold() with perform() -> Option B
Quick Check:
Chain clickAndHold().perform() to run action [OK]
Hint: Always chain perform() immediately after action method [OK]
Common Mistakes:
Calling perform() separately without chaining
Assuming perform() runs all previous actions
Not understanding fluent interface chaining
5. You want to test a drag-and-drop feature where you click and hold an element with id source, move it to an element with id target, then release. Which of the following code snippets correctly performs this using Selenium Java?
hard
A. new Actions(driver).dragAndDrop(By.id("source"), By.id("target")).perform();
B. new Actions(driver).click(driver.findElement(By.id("source"))).moveToElement(driver.findElement(By.id("target"))).click().perform();
C. new Actions(driver).clickAndHold(driver.findElement(By.id("source"))).moveToElement(driver.findElement(By.id("target"))).release().perform();
D. new Actions(driver).clickAndHold(By.id("source")).moveToElement(By.id("target")).release().perform();
Solution
Step 1: Understand drag-and-drop with clickAndHold
To drag and drop manually, you click and hold the source element, move to the target element, then release the mouse button.
Step 2: Analyze each option
new Actions(driver).clickAndHold(driver.findElement(By.id("source"))).moveToElement(driver.findElement(By.id("target"))).release().perform(); correctly chains clickAndHold(source), moveToElement(target), release(), and perform(). new Actions(driver).click(driver.findElement(By.id("source"))).moveToElement(driver.findElement(By.id("target"))).click().perform(); uses click instead of clickAndHold, new Actions(driver).dragAndDrop(By.id("source"), By.id("target")).perform(); incorrectly passes By locators instead of WebElements, new Actions(driver).clickAndHold(By.id("source")).moveToElement(By.id("target")).release().perform(); incorrectly passes By locators instead of WebElements.
Final Answer:
new Actions(driver).clickAndHold(driver.findElement(By.id("source"))).moveToElement(driver.findElement(By.id("target"))).release().perform(); -> Option C
Quick Check:
clickAndHold + moveToElement + release = drag and drop [OK]
Hint: Use WebElements, chain clickAndHold, moveToElement, release, perform [OK]
Common Mistakes:
Passing By locators instead of WebElements
Using click instead of clickAndHold for drag
Confusing dragAndDrop() with manual actions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.