Forum Discussion

aceishigh's avatar
aceishigh
Contributor
14 years ago

Unable to locate element on IE using EvaluateXPath

Hi All,

I'm trying to select an option from a drop-down menu by using the value attribute and not the visible text. What I'm doing is grapping the @title attribute and passing it to the ClickItem function. However, this doesn't seem to be working. I tested my xpath expression on Firefox and it's returning the right value.

Any idea what I'm doing wrong?

Thanks for any help offered.

J.



***************

Drop Down Menu

***************

<select id="language" class="orionSelect" size="1" onchange="__changeLanguage();">

<option selected="selected" value="en" title="English"> English </option>

<option value="en_GB" title="English (United Kingdom)"> English (United Kingdom) </option>

<option value="zh_CN" title="Chinese (Simplified)"> Chinese (Simplified) </option>

<option value="zh_TW" title="Chinese (Traditional)"> Chinese (Traditional) </option>

<option value="fr" title="French"> French </option>

<option value="de" title="German"> German </option>

<option value="ja" title="Japanese"> Japanese </option>

<option value="ko" title="Korean"> Korean </option>

<option value="ru" title="Russian"> Russian </option>

<option value="es" title="Spanish"> Spanish </option>

</select>



******************

TC Script

******************

Sub Login

  Dim iexplore

  Dim page

  Dim textbox

  Dim passwordBox

  Dim obj

  Dim MenuItem

  Dim strMenuItem

  TestedApps.Page1.Run

  Set iexplore = Aliases.IEXPLORE

  Call iexplore.ToUrl("https://my-test-server")

  ' Obtain the Page object

  Set page = Sys.Process("iexplore").Page("https://my-test-server")

  ' Evaluate xpath to the select menu element with id=langauge

  obj = page.EvaluateXPath("//*[@id='language']")

 

  ' MenuItem is the specific option title that I want to select

  MenuItem = page.EvaluateXPath("//SELECT[@id='language']//OPTION[@value='fr']/@title")

  ' Convert to a string

  strMenuItem = CStr(MenuItem(0))

 

  ' Check the result

  If VarType(obj) <> varEmpty Then

    ' If the element was found, click it

    ' obj(0).Click ' Note we refer to the array item

    obj(0).ClickItem(strMenuItem)

  Else

    ' If the element was not found, post a message to the log

    Log.Message "The element was not found"

  End If

End Sub

2 Replies

  • Hi Jack,



    When you query a web element's attribute using XPath, it returns the attribute object (IHTMLDOMAttribute for IE, Attr for Firefox and so on) rather than the attribute value.



    To access the attribute value given the attribute object, you can use its value property:

    /@title")

    ' Convert to a string

    strMenuItem = CStr(MenuItem(0).value)


    Or you can query the OPTION element instead and then access its title attribute directly:

    ")

    ' Convert to a string

    strMenuItem = CStr(MenuItem(0).title)
  • Thanks very much Helen for your reply. The second option worked perfectly.

    Much appreciated.

    J.