Add an explicit wait after you click your menu and then once the drop-down element is present refer below steps:
https://selenium.dev/docs/site/en/support_packages/working_with_select_elements/
Please refer to the above document:
You can use the select class in ruby to achieve this:
select_element = driver.find_element(id: 'couCode')
select_object = Select(select_element)
To select using text:
select_object.select_by(:text, 'URUGUAY')
Using Value:
select_object.select_by(:value, 'UY')
Using index:
select_object.select_by(:index, 1)
NoteIssue with your code:
This also doesn't work, throws exception
options.find_element(:value, "GB").click
There is no element locator called "value" that is supported by find_elemnet method. So you are getting argument error saying :value is wrong.
Below are the only supported locators
{
:class => 'ClassName',
:class_name => 'ClassName',
:css => 'CssSelector',
:id => 'Id',
:link => 'LinkText',
:link_text => 'LinkText',
:name => 'Name',
:partial_link_text => 'PartialLinkText',
:tag_name => 'TagName',
:xpath => 'Xpath',
}
THis throws error :
You could make below code work by changing:
options.each { |option| option.click if (option.value == 'GB')}
To
options.each { |option| option.click if (option.attribute("value")== 'GB')}
Because the option is just a web element and does not contain any method called value. So it throws an error if you call option.value.
This does not work and does throw error also,
country.click if country.text == 'UNITED KINGDOM'}
Because it's not an element inside the drop-down, but won't throw any error as syntax is correct.
Prints just comma:
Here,
options.each { |a|
puts a.text + ', '
}
instead of puts a.text + ', '
. Try, puts a.attribute("textContent") +','