<select class="selectCity">
<option></option>
<option value="Paris">Paris</option>
<option>New York</option>
<option>London</option>
</select>
Select op1 = new Select(driver.findElement(By.xpath("(//*[#id='cityTable']//*[contains(#class,'selectCity')])")));
List<WebElement> allSelectedOptions = op1.getAllSelectedOptions();
WebElement firstSelectedOption = op1.getFirstSelectedOption();
System.out.println("op1!!!!!"+firstSelectedOption.getText());
The user selected option on the web page is London.
Put the output is op1!!!!!
How to find the option that has been selected on the web page?
Thanks in advance!
Selenium.getSelectedLabel("//string locator");
The above code helps in knowing the option witch is currently selected and visible from the drop down list.
String locator of drop down box can by anything eg:- name,id,xpath
EG : Selenium.getSelectedLabel("name=productIdxSel");
Related
Actions a= new Actions(driver);
WebElement mainmenu=driver.findElement(By.xpath(".//*[#id='yui-gen2']/a"));
a.moveToElement(mainmenu).build().perform();
WebElement Sub = driver.findElement(By.xpath(".//*[#id='helpAbout']"));
a.moveToElement(Sub).build().perform();
Sub.click();
Code couldn't able to click on submenu it just stops at 3rd line.
With selenium you should be able to just do the following:
Select variableName = new Select(DropDownElementLocator);
variableName.selectByVisibleText("Whatever");
// or
variableName.selectByIndex(1);
Your code is 90% correct, just replace following code:
a.moveToElement(Sub).click().perform();
build() method works for hover on element,and after hover on it we have to click element.
Once you Mouse Hover over the element identified as By.xpath(".//*[#id='yui-gen2']/a") and therefafter invoke moveToElement(mainmenu), build(), perform(), at this stage the element identified as By.xpath(".//*[#id='helpAbout']") is visible and interactable. So you need to invoke click() directly as follows :
Actions a= new Actions(driver);
WebElement mainmenu=driver.findElement(By.xpath(".//*[#id='yui-gen2']/a"));
a.moveToElement(mainmenu).build().perform();
WebElement Sub = driver.findElement(By.xpath(".//*[#id='helpAbout']"));
Sub.click();
import org.openqa.selenium.support.ui.Select;
WebElement selectElement;
selectElement= driver.findElement(By.id("yourElementId"));
or
selectElement driver.findElement(By.xpath("yourElementXpath"));
Select selectObject = new Select(selectElement);
The Select object will now give you a series of commands that allow you to interact with a element. First of all, there are different ways of selecting an option from the element.
<select>
<option value=value1>Bread</option>
<option value=value2 selected>Milk</option>
<option value=value3>Cheese</option>
</select>
// Select an <option> based upon the <select> element's internal index
selectObject.selectByIndex(1);
// Select an <option> based upon its value attribute
selectObject.selectByValue("value1");
// Select an <option> based upon its text. Example: Bread
selectObject.selectByVisibleText("Bread");
I have a vb program which uses the built in web browser to find a specific drop down menu on a site and chose the size which the user has entered. The website changes the value for each different item so I must find the correct option using the following code:
Dim element As HtmlElement = WebBrowser1.Document.GetElementsByTagName("select").Cast(Of HtmlElement).First(Function(el) el.GetAttribute("name") = "size")
element.GetElementsByTagName("option").Cast(Of HtmlElement).First(Function(el) el.InnerText = Form1.cmbSize1.Text).SetAttribute("selected", "selected")
This works to set the dropdown menu to the correct size if it is available but if for example the user enters size as : 'Small' and it isn't present I would like the program to add the next size for example : 'Medium'
<select id="size" name="size">
<option value="26881">Medium</option>
<option value="26882">Large</option>
<option value="26883">XLarge</option>
</select>
By default the website loads with the default size 'Medium' if the user entered size but I have a function which adds the item to the users cart afterwards and if the size entered isn't present for some reason it will not add to the cart
the code for adding to cart is:
For Each addtobasket As HtmlElement In WebBrowser1.Document.GetElementsByTagName("input")
If addtobasket.OuterHtml.Contains("add to basket") Then
addtobasket.InvokeMember("click")
Return
End If
Next
Thanks in advance :)
I'm trying to automate loading data using vb.net, but there comes a point where I select values of various combobox. Seeing the code of the page I find that the combobox has a format similar to this(its just an example):
<select id="BirthMonth" name="BirthMonth">
<option value="">Month</option>
<option value="01" >January</option>
<option value="02" >February</option>
<option value="03" >March</option>
<option value="04" >April</option>
fails to work with WebBrowser1.Document.GetElementById ("Name combo") SetAttribute ("OPTION", "March"), searching the Internet I found a solution where inter is a parameter that contains the value.:
Public Sub selector(ByVal inter)
Dim option_ As HtmlElementCollection
option_ = WebBrowser1.Document.GetElementsByTagName("option")
For Each option__ As HtmlElement In option_
If option__.InnerHtml = inter Then
option__.SetAttribute("selected", "True")
End If
Next
End Sub
which if you choose value, but the page does not take it, so it does not refresh the other Combobox.
Anyone know how to do to select the value, so the page can trigger the action?
PS: must be in the name of the field by value.
Thank you very much
Bye!
PS: I solved the problem just adding this lines after the selection:
WebBrowser1.AllowNavigation = True
WebBrowser1.Document.Forms(0).InvokeMember("submit")
Its solved just adding this lines after the selection:
WebBrowser1.AllowNavigation = True
WebBrowser1.Document.Forms(0).InvokeMember("submit")
This question already has answers here:
Selenium WebDriver - get options from hidden select [closed]
(2 answers)
Closed 7 months ago.
I'm having a big problem with select on one page.
Code:
<select name="fw3k_ad_input_et_type_group" class=""
id="_id_fw3k_ad_input_et_type_group"
onchange=" eurotax.change_type_group( this.value ); "
style="display: none; ">
<option value="0"> --- odaberite tip --- </option>
<option value="-1" class="special">> nema mog tipa </option>
<option value="16390">CD</option>
<option value="17605">S</option>
<option value="17636">SE</option>
</select>
--- odaberite tip ---
Select is hidden and a href="" is visible part that changes its text depending on a selected option.
I don't know how to manage that. I can get all options with JavascriptExecutor and I can use a.click() to view dropdown box but I don't know how to click on some option.
I have tried to use Select class and .getOptions() method but it doesn't work with hidden select and I cannot change <a href=""> text.
A little confused with the question but have you you tried
WebElement element = driver.findElement(By.id("fw3k_ad_input_et_type_group"));
Select select = new Select(element);
Then use either
select.selectByValue(value);
select.selectByVisibleText(text);
select.selectByIndex(index);
1st way:
it is not the problem to click any element using the same js. As you know how to get any option the last actions remaning is to perform a click.
This should work for you:
WebElement hiddenWebElement =driver.findElement(By(..selector of the element....));
((JavascriptExecutor)driver).executeScript("arguments[0].click()",hiddenWebElement);
2nd way:
String cssSelector= ...//i gave them in your previous question
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+cssSelector+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
3rd way:
using actions builder, advanced user actions API. You can read about it here
And code will be smth like that:
WebElement mnuElement;
WebElement submnuElement;
mnEle = driver.findElement(By.Id("mnEle")).click();
sbEle = driver.findElement(By.Id("sbEle")).click();
Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.moveToElement(mnEle).Perform();
// Giving 5 Secs for submenu to be displayed
Thread.sleep(5000L);
// Clicking on the Hidden SubMenu
driver.findElement(By.Id("sbEle")).click();
You can also some additional info here
Hope this somehow helps you)
driver.findElement(By.name("_id_fw3k_ad_input_et_type_group")).sendKeys("16390");
worked for me for something very similar.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Selenium WebDriver - hidden select and anchor
Before I post my question I just want to tell that I am new to Selenium..
I am trying to select an option from a dropdown. The options display when clicked on the down arrow in the dropdown box. But, when checked in the Firebug, the display style was "none" and when trying to select the option using JUnit webdriver code by using click method in Eclipse, it did not work and it gave the exception - "org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with".
Please refer to the attached image for the dropdown and HTML tags.
<select class="size-dropdown mediumSelect selectBox" name="skuAndSize" style="display: none;">
<option value=""></option>
<option value="2545672:S" name="skuId"> S</option>
<option value="2545673:M" name="skuId"> M</option![enter image description here][1]>
<option value="2545674:L" name="skuId"> L</option>
<option value="2545675:XL" name="skuId"> XL</option>
<option value="2545676:XXL" name="skuId"> XXL</option>
<option value="2545677:XXXL" name="skuId"> XXXL</option>
<option value="2545678:XXXXL" name="skuId"> XXXXL</option>
</select>
I looked at this link before posting this question - Selenium WebDriver - hidden select and anchor
But, since I am just starting I am not able to understand clearly.
Note: The same worked in IDE when used clickAt method. But in Webdriver the clickAt method is not present. Can anyone help me in this. Thanks!
Well the matter is Selenium is unable to interact with invisible ( disabled) elemnts. So you need to make element visible. BAsic idea: to make dropdown roll down , then wait with driver.manage.timeout(...) and then click on the appear needed element in dropdown.
Or you can use javascript to click element directly without preceeding dropdown roll down. Js is able to cope with it.
So this approach ALWAYS works:
css1=select[class="size-dropdown mediumSelect selectBox"]>option[value=""]
css2=select[class="size-dropdown mediumSelect selectBox"]>option[value="2545672:S"]
css2=select[class="size-dropdown mediumSelect selectBox"]>option[value="value="2545673:M"]
//.... and so on.....
public void jsClickOn(String cssSelector){
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+cssSelector+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
}
jsClickOn(css1);
jsClickOn(css2);
jsClickOn(css3);
Another way:
using actions builder, advanced user actions API. You can read about it here And code will be smth like that:
WebElement mnuElement;
WebElement submnuElement;
mnEle = driver.findElement(By.Id("mnEle")).click();
sbEle = driver.findElement(By.Id("sbEle")).click();
Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.moveToElement(mnEle).Perform();
// Giving 5 Secs for submenu to be displayed
Thread.sleep(5000L);
// Clicking on the Hidden SubMenu
driver.findElement(By.Id("sbEle")).click();
But also pay attention on the way how you found css selectors, xpaths verifying it in e.g. firepath, addon to firebug in ffox.
Hope this helps you)
See this screen as example of locating dropdown options.