0
0
Selenium Javatesting~10 mins

Getting and setting attributes in Selenium Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the value of the "href" attribute from the link element.

Selenium Java
String link = driver.findElement(By.id("homeLink")).[1]("href");
Drag options to blanks, or click blank then click option'
AgetAttribute
BgetText
CgetCssValue
DgetTagName
Attempts:
3 left
💡 Hint
Common Mistakes
Using getText() instead of getAttribute() to get attribute values.
Using getCssValue() which is for CSS properties, not attributes.
2fill in blank
medium

Complete the code to set the "value" attribute of an input element using JavaScript executor.

Selenium Java
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].[1] = 'test';", element);
Drag options to blanks, or click blank then click option'
AsetValue
BsetAttribute
Cvalue
DinnerText
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call setAttribute as a property.
Using innerText which changes visible text, not input value.
3fill in blank
hard

Fix the error in the code to correctly get the "class" attribute of a web element.

Selenium Java
String className = element.[1]("class");
Drag options to blanks, or click blank then click option'
AgetAttribute
BgetText
CgetClass
DgetCssValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using getClass() which is a Java method, not Selenium WebElement method.
Passing "className" instead of "class" to getAttribute.
4fill in blank
hard

Fill both blanks to set the "placeholder" attribute of an input element using JavaScript executor.

Selenium Java
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].[1]('[2]', 'Enter name');", element);
Drag options to blanks, or click blank then click option'
AsetAttribute
BgetAttribute
Cplaceholder
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using getAttribute instead of setAttribute to set values.
Passing wrong attribute names like "value" instead of "placeholder".
5fill in blank
hard

Fill all three blanks to create a map of element IDs to their "title" attribute values for elements in the list.

Selenium Java
Map<String, String> titles = elements.stream()
  .collect(Collectors.toMap(e -> e.[1]("id"), e -> e.[2]("[3]")));
Drag options to blanks, or click blank then click option'
AgetId
BgetAttribute
Ctitle
DgetText
Attempts:
3 left
💡 Hint
Common Mistakes
Using getText() instead of getAttribute("title") for attribute values.
Using wrong method names like getId() which is not standard in Selenium.