The selector #loginForm .submit-btn finds any element with class submit-btn inside the element with id loginForm. Option C looks for an element with id inside a class, which is incorrect. Option C looks for a direct child button which may not always be correct if the button is nested deeper. Option C looks for a button with id 'loginForm' which is wrong.
WebElement element = driver.findElement(By.cssSelector("div.content > p.highlight"));
System.out.println(element.getText());The findElement method throws NoSuchElementException if the element is not found. The code tries to print the text of the element, but if the element does not exist, it will not reach the print statement and instead throw an exception.
WebElement element = driver.findElement(By.cssSelector(".nav-item.active"));element.isDisplayed() returns true if the element is visible. Using assertTrue with this method correctly asserts visibility. Option D wrongly compares text to "true". Option D only checks element is not null but does not confirm visibility. Option D asserts the element is disabled, which is unrelated.
<ul id="menu">
<li class="item">Home</li>
<div>
<li class="item active">About</li>
</div>
</ul>The code below throws NoSuchElementException. Why?
driver.findElement(By.cssSelector("ul#menu > li.active"));- and
- elements and the meaning of > in CSS selectors.
The selector ul#menu > li.active looks for
Option A correctly uses Duration.ofSeconds(timeoutSeconds) for the timeout and waits for presence of element located by the given selector. Option A uses an outdated constructor without Duration, which is deprecated. Option A waits for element to be clickable, which is more restrictive than presence. Option A waits for visibility, which may fail if element is present but hidden.