2
votes

I'm trying to click on "Mr" from the drop down list I've tried a combination of things but non of them seem to work.

I've even tried xpath which is usually reliable but for this case its failing.

$browser.element(:xpath, "/html/body/div[1]/div[1]/div[1]/div/div[2]/div[1]/div[2]/div/div[2]/div/div[2]/div[2]/form/div[2]/div/div[2]/div/div/div/div/div/ul/li[2]/a").click 

click on MR

2

2 Answers

5
votes

The XPath suggested by Saurabh Gaur, can be written in a more readable Watir-like fashion using:

$browser.ul(class: 'dropdown-menu').link(text: 'Mr').click

Note that this assumes that there is only one ul element with class dropdown-menu. If there are multiple, you will need to scope the search to the specific dropdown using an element that likely exists higher in the DOM.

However, given there is likely only one link with text "Mr", you can probably get away with simply:

$browser.link(text: 'Mr').click

Given the link is a dialog that switches from hidden to visible, you may need to also wait:

$browser.link(text: 'Mr').when_present.click
1
votes

Your xPath is positional which depends on element position.. it will not work if elements are change their position means adding some elements after some action on the page.

After seeing your attached image I have generated following xPath as below :-

//ul[contains(@class, 'dropdown-menu')]/descendant::span[contains(.,'Mr')]/parent::a

Try with this xPath.. May be it will work...:)