How to enable watir-WebDriver to support selecting a chinese drop-down menu

Source: Internet
Author: User

When we transplanted the waitr code to watir-WebDriver today, we found that the Select. Select (text) method of watir-WebDriver does not seem to support Chinese text.

That is to say, the Select method of watir-WebDriver cannot select the Chinese option.

After studying the source code, we found that the Select. Select method is implemented as follows:

Def select (str_or_rx) select_by: Text, explain # The select method calls the select_by method def select_by (how, str_or_rx) assert_exists case str_or_rx when string, numeric select_by_string (how, limit) when Regexp select_by_regexp (how, str_or_rx) else raise typeerror, "Expected string or Regexp, got # {str_or_rx.inspect }: # {str_or_rx.class} "endend # Call the select_by_string method def select_by_strin when the select parameter is string. G (how, string) XPath = option_xpath_for (how, string) if multiple? Elements = @ element. find_elements (: XPath, XPath) no_value_found (string) If elements. Empty? Elements. Each {| E. Click unless E. Selected?} Elements. first. text else begin E = @ element. find_element (: XPath, XPath) rescue WebDriver: Error: nosuchelementerror no_value_found (string) end e. click unless E. selected? Safe_text (e) endend # The select_by_string method calls the option_xpath_for method to generate the xpathdef option_xpath_for (how, string) string = xpathsupport for this option. escape string case how when: Text ". // option [normalize-space () =#{ string} Or @ label =#{ string}] "When: value ". // option [@ value =#{ string}] "else raise error," unknown how: # {how. inspect} "endend

It can be seen that the Select. Select (text) method uses the text parameter to construct a value". // Option [normalize-space () =#{ text }"Or"@ Label = # {string}]"And then useFind_elementMethod to find the option to be selected.

After all, the Select method needs to construct XPath and then use XPath to locate the object. Therefore, it does not support Chinese characters.Probably because the element Locating Method of WebDriver does not support XPath with Chinese characters. Of course, the above reason is just speculation. As for the real ultimate reason, we need to carefully study the find_element method. Due to limited time, we will not be entangled in this issue here.

Of course, since watir-WebDriver may not support native select methods to select Chinese options, we need to write our own localization methods that support Chinese options. The Code is as follows:

Watir: option. class_eval do def value assert_exists @ element. attribute (: Value) end watir: select. class_eval do def getallcontents contents = [] options. each do | o | contents. push O. text rescue next end # Each contents end # def alias: get_all_contents: getallcontents def getallvalues values = [] options. each do | o | values. push O. value end values end # def alias: get_all_values: getallvalues # return all options in the select list # provide value => text key value pair def text_to_value the_hash = {} values = getallvalues getallcontents. each_with_index do | T, I | the_hash [T] = values [I] end the_hash end # def my_select text # You can also directly overwrite the native select method begin select text rescue select_value text_to_value [text] end # class eval

The idea of the above Code is:First, add the Value Method to the option class, and then add the method to obtain the text and value of all options of the current select_list in the select class. When the native select method cannot select option, obtain the value corresponding to this option, and then use the select_value method to select. In general, this method can replace the native select method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.