Which of the following XPath expressions would select an element whose id attribute starts with "user"?
a) //input[contains(@id, 'user')]
b) //input[ends-with(@id, 'user')]
c) //input[starts-with(@id, 'user')]
d) //input[contains(text(), 'user')]
Answer: c) //input[starts-with(@id, 'user')]
How do you select an element using XPath that contains specific text "Login"?
a) //button[text()='Login']
b) //button[contains(text(), 'Login')]
c) //button[@text='Login']
d) //button[has-text()='Login']
Answer: b) //button[contains(text(), 'Login')]
Which XPath expression would you use to select the last input element in a form?
a) //input[last()]
b) //input[last()-1]
c) //input[final()]
d) //input[end()]
Answer: a) //input[last()]
How can you select the second input element inside a form using XPath?
a) //input[2]
b) //input[position()=2]
c) //form/input[2]
d) All of the above
Answer: d) All of the above
What does the XPath expression //* select?
a) All elements in the document
b) All child elements of the root
c) All elements with an attribute
d) All text nodes
Answer: a) All elements in the document
Which XPath expression will select any element with any attribute?
a) //*[@*]
b) //*[@]
c) //*[*]
d) //[@*]
Answer: a) //*[@*]
What is the purpose of using the or operator in XPath expressions?
a) To select elements with multiple attributes
b) To select elements that satisfy at least one of multiple conditions
c) To select elements that do not have a specific attribute
d) To select all child nodes
Answer: b) To select elements that satisfy at least one of multiple conditions
Which XPath expression uses the or operator correctly to select an input element with either id='username' or name='username'?
a) //input[@id='username' or @name='username']
b) //input[@id='username' || @name='username']
c) //input[id='username' or name='username']
d) //input[@id='username'] | //input[@name='username']
Answer: a) //input[@id='username' or @name='username']
Which XPath expression will select a div that contains an h2 child with the text "Welcome"?
a) //div[h2='Welcome']
b) //div[h2[text()='Welcome']]
c) //div[contains(h2, 'Welcome')]
d) //div[contains(h2/text(), 'Welcome')]
Answer: b) //div[h2[text()='Welcome']]
Which XPath expression would select any span element that has an id starting with "msg" and also contains the text "error"?
a) //span[@id='msg' and text()='error']
b) //span[starts-with(@id, 'msg') and contains(text(), 'error')]
c) //span[starts-with(@id, 'msg') or contains(text(), 'error')]
d) //span[@id^='msg' and text*='error']
Answer: b) //span[starts-with(@id, 'msg') and contains(text(), 'error')]
Which XPath expression correctly identifies any child or subchild element with the tag name a?
a) //a/*
b) //a
c) //*/a
d) //a//*
Answer: d) //a//*
How would you select all input elements that are direct children of a form element?
a) //form//input
b) //form/input
c) //form[child::input]
d) //form/input/*
Answer: b) //form/input
To find elements where the tag name can be anything but must have a specific attribute, which XPath should be used? a) //*[@attribute='value']
b) //element[@attribute='value']
c) //*[tagname='*' and @attribute='value']
d) //[@attribute='value']
Answer: a) //*[@attribute='value']
What does the XPath expression //*[contains(@class, 'button') and contains(@id, 'submit')] do?
a) Selects all elements with class button and id containing submit
b) Selects only button elements with id containing submit
c) Selects elements with both class and id containing button and submit respectively
d) Throws an XPath syntax error
Answer: a) Selects all elements with class button and id containing submit
Which of the following XPath expressions correctly uses the position() function?
a) //div[position() > 1]
b) //div[position()>1]
c) //div[position()>=1]
d) All of the above
Answer: d) All of the above
How would you write an XPath to select any element that has an attribute ending with the value "name"?
a) //*[@*='name']
b) //*[ends-with(@*, 'name')]
c) //*[@attribute='name']
d) //*[contains(@*, 'name')]
Answer: b) //*[ends-with(@*, 'name')]
Which XPath syntax is used to match all elements with a class attribute of either header or footer?
a) //*[@class='header' or @class='footer']
b) //*[@class='header' and @class='footer']
c) //[@class='header' | @class='footer']
d) //*[@class*='header,footer']
Answer: a) //*[@class='header' or @class='footer']
What will //button[@type='submit' and @name='login'] select?
a) All buttons with type='submit' or name='login'
b) All buttons with both type='submit' and name='login' attributes
c) The first button with type='submit'
d) Any element with type='submit' or name='login'
Answer: b) All buttons with both type='submit' and name='login' attributes
No comments:
Post a Comment