Could you please help me in selecting drop down value in selenium using java code.
My issue is that in HTML code, i do not have SELECT class.
This is the code i have tried :
List<WebElement> elements = driver.findElement(By.id("Some Value"));
for (WebElement element: elements){
new Actions(driver).sendKeys(Keys.Arrow_Down).perform();
if(Element.getText().equals("Cliam Document")){
element.click();
}
}
Below is html snippet :
<input class="dijitReset dijitInputInner" autocomplete="off" data-dojo-attach-point="textbox,focusNode" role="textbox" aria-autocomplete="both" aria-required="true" tabindex="0" id="ecm_widget_AddContentItemGeneralPane_0_entryTemplateSelector" value="" aria-invalid="true" type="text">
<span class="dijitPlaceHolder dijitInputField">Enter or select an entry template</span>
<input name="ecm_widget_AddContentItemGeneralPane_0_entryTemplateSelector" value="" type="hidden"></div>
you can try following code (Here I'm assuming that dropdown options appear after clicking the dropdown field):
WebElement yourDropdown = driver.findElement(By.cssSelector("#ecm_widget_AddContentItemGeneralPane_0_entryTemplateSelector"));
yourDropdown .click();
List<WebElement> elements = driver.findElements(By.cssSelector("#ecm_widget_AddContentItemGeneralPane_0_entryTemplateSelector input"));
for (WebElement element: elements){
if(Element.getText().equals("Cliam Document")){
element.click();
}
}
Follow these steps:
Step 1: Enter value in the input field.
driver.findElement(By.xpath("Enter xpath here")).sendkeys("enter text here");
Step 2: Now to select value from the list
List<WebElement> elements = driver.findElements(By.xpath("Some Value"));
for (WebElement element: elements){
String element_text = element.gettext();
if(element_text.equals("Cliam Document")){
element.click();
}
}
Follow Below steps, will be able to select value from dropDown
Step 1.
WebElement dropdownTab = driver.findElements(By.xpath("dropdown locator")).click(); //Click on the dropdown tab
Step2:
List options = driver.findElements(By.tag_name('option')); //Get all the options
Step 3:
for (WebElement element: options){
if(options.getText().equals("Text Name which needs to be selected")){
element.click();
break;
}
}
This should work, if still you are facing issue - Please provide the url where you are trying...will post the exact code
Related
HTML:
"Add to cart" object
<button class="button spin-button prod-ProductCTA--primary button--primary" data-automation-id="button" data-tl-id="ProductPrimaryCTA-cta_add_to_cart_button" type="button"><span class="button-wrapper"><span class="spin-button-children">Add to cart</span></span></button>
"Get in stock alert" object
<button class="button spin-button prod-ProductCTA--primary button--primary" data-automation-id="button" data-tl-id="cta_oos_button" aria-expanded="false" aria-label="get in stock alert" role="button" tabindex="0" type="button"><span class="button-wrapper"><span class="spin-button-children">Get in-stock alert</span></span></button>
I do not want to click "Add to cart". I just want to store locator info in WebElement "addToCart". Because objects have very identical properties, I chose this unique property "data-tl-id" but did not work.
Web Driver
WebElement addToCart = driver.findElement(By.xpath("//button[#data-tl-id='ProductPrimaryCTA-cta_add_to_cart_button']"));
Below worked for me but both above objects have the same class name so i cant use the class name.
WebElement addToCart = driver.findElement(By.xpath("//span[#class='button-wrapper']"));
I also tried below ways but none worked for me.
WebElement addToCart = driver.findElement(By.xpath("//span[text()='Add to cart'"));
WebElement addToCart = driver.findElement(By.xpath("//span[contains(#text,'Add to cart')]"));
I get error "No such element: Unable to locate element: ". How to locate the "Add to cart" object?
Your xpath is wrong WebElement addToCart = driver.findElement(By.xpath("//span[text()='Add to cart'"));
Try below solution
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement webElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Add to cart']")));
System.out.println("Printing "+webElement.getText());
Or you can also try xpath with contains
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement webElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(text(),'Add to cart')]")));
System.out.println("Printing "+webElement.getText());
since this xpath worked for you,
WebElement addToCart = driver.findElement(By.xpath("//span[#class='button-wrapper']"));
Use this to uniquely identify WebElement
driver.findElement(By.xpath("//button[contains(#data-tl-id,'cart')]/span[#class='button-wrapper']"))
you can also use this xpath
//span[contains(text(),'Add to cart')]
i have an HTML element which is not select element. so i want to verify all the dropdown list using soft assert.
This is the code which i tried but it works only for select HTML element.
String[] exp = {"--None--","Open","Closed","Priority-Reopened","Researching","Updated","Escalated"};
WebElement dropdown = threadWebDriver.get().findElement(By.id("ddlNights"));
Select select = new Select(dropdown);
List<WebElement> options = select.getOptions();
for(WebElement we:options)
{
boolean match = false;
for (int i=0; i<exp.length(); i++){
if (we.getText().equals(exp[i]){
match = true;
}
}
Assert.assertTrue(match);
}
HTML element for this is given below:-
<a aria-required="true" class="select" aria-disabled="false" aria-describedby="2295:0-label" aria-haspopup="true" tabindex="0" role="button" title="" data-aura-rendered-by="2305:0" href="javascript:void(0);" data-interactive-lib-uid="9">Open</a>
How i can verify the list of drop down values?
I would take another approach of checking if the dropdown have any of the values specified using xpath like this.
//*[#id='ddlNights'][contains('--None--,Open,Closed,Priority-Reopened,Researching,Updated,Escalated',a)]
Screenshot:
You can check if the element exist with the above xpath. That validates true if any of the list items present otherwise it will be false. You have to handle that assertion using softAssertion.
I am trying to select the value from the drop down. Below is the HTML of that dropdown on the login screen.
<div class="form-group">
<label for="j_companyname">Company</label>
<select id="j_companyname" name="companyname" size="1" onclick="onAccountLostFocus();" style="cursor: pointer;"><option value="AUTO_DEPLOYER">AUTO_DEPLOYER</option><option value="ES188CLIENT1">ES188CLIENT1</option><option value="ES188CLIENT10">ES188CLIENT10</option><option value="ES188CLIENT11">ES188CLIENT11</option><option value="ES188CLIENT12">ES188CLIENT12</option><option value="ES188CLIENT13">ES188CLIENT13</option><option value="ES188CLIENT14">ES188CLIENT14</option><option value="ES188CLIENT15">ES188CLIENT15</option><option value="ES188CLIENT16">ES188CLIENT16</option><option value="ES188CLIENT17">ES188CLIENT17</option><option value="ES188CLIENT18">ES188CLIENT18</option><option value="ES188CLIENT19">ES188CLIENT19</option><option value="Primatics">Primatics</option></select>
<img src="ui/img/ajax-loader.gif" style="margin: 3px; position: absolute; display: none;">
"
I tried to locate element via different locators but unable to pick any value. My script is:
if( driver.findElement(By.id("j_companyname")).isEnabled()){
System.out.println("Element is Enable");
WebElement select = driver.findElement(By.id("j_companyname"));
List<WebElement> options = select.findElements(By.tagName("option"));
for (WebElement option : options)
{
if("ES188CLIENT16".equals(option.getText()))
// option.click();
option.submit();
}
}
else{
System.out.println("Element is Disabled");
}
Please help.
Pleas look below code
Select drpCountry = new Select(driver.findElement(By.id("j_companyname")));
drpCountry.selectByVisibleText("ES188CLIENT16");
// these also can help you
//drpCountry.selectByIndex("givenindex");
//drpCountry.selectByValue("ES188CLIENT16");
As you are trying to select the value from the drop down, lets assume we have to select AUTO_DEPLOYER from the dropdown. To select AUTO_DEPLOYER from the dropdown you can use the following code block :
WebElement my_select = driver.findElement(By.id("j_companyname"));
Select select = new Select(my_select);
new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeSelected(By.xpath("//select[#id='j_companyname']//option[contains(.,'AUTO_DEPLOYER')]")));
select.selectByVisibleText("AUTO_DEPLOYER");
Can anyone guide me how to select specific element from list view
from using below code.
<div id="menu" class="tabalign k-widget k-reset k-header k-menu k-menu-horizontal" data-role="menu" tabindex="0" role="menubar" aria-activedescendant="menu_mn_active">
<li class="k-item k-state-default k-first mainMenu2" role="menuitem" id="menu_mn_active"><span class="k-link"><img class="k-image" alt="" src="/Images/Common/Module_2.png"><span id="2">Profiles</span></span></li>
<li class="k-item k-state-default mainMenu3" role="menuitem"><span class="k-link"><img class="k-image" alt="" src="/Images/Common/Module_3.png"><span id="3">Contacts</span></span></li>
And I have tried below code but it is not worked for me:
WebElement element = driver.findElements(By.xpath("//*[#id=\"menu_mn_active\"]"));
element.click();
Try this code in Java (it selects first element from dropdown):
driver.findElement(By. id("menu")).click();
List<WebElement> options = driver.findElements(By. cssSelector(".k-item.k-state-default"));
for (WebElement opt : options) {
if (opt.getText().equals("Profiles")) {
opt.click();
}
}
You can use like this:
First get location of all elements:
List<WebElement> element = driver.findElements(By.xpath("//*[#id=\"menu_mn_active\"]"));
For the first element:
element.get(0).click(); // for first element
For the second element:
element.get(1).click(); // for second element
Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath("//*[#id='ctl00_Sitemap1_HyperLink1']"));
action.moveToElement(we).build().perform();
WebElement tmpElement= driver.findElement(By.xpath("//*[#id='ctl00_Sitemap1_HyperLink1']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", tmpElement);
List<WebElement> dd_list = driver.findElementsByXPath("//*[#id='masterNavigation']/ul/li[1]/ul/li");
for (WebElement ele : dd_list)
{
System.out.println("Values " + ele.getAttribute("innerHTML"));
if (ele.getAttribute("innerHTML").contains("Event Dashboard")) {
ele.click();
break;
}
}
}
Hi I am trying to Automate bootstrap drop-down menu. It's visibility is hidden by default.Once you hover mouse on it, its visibility property shows visible.I am able to click on drop-down , but after clicking on drop-down my selenium script is not selecting value from drop-down.
Error: Exception in thread "main"
org.openqa.selenium.ElementNotVisibleException: Cannot click on
element
HTML Code Snippet
<a class="ui-button-text-icons" id="ctl00_Sitemap1_HyperLink1" href="javascript:void(void);">
<span style="padding-right: 1.3em;">Dashboards</span>
<span class="ui-button-icon-secondary ui-icon ui-icon-triangle-1-s"></span>
</a>
<ul style="visibility: hidden;">
<li class="first featureGranted">
Classic Dashboard
</li>
</ul>
Few things
You don't need to traverse though all li elements to find your desire element you can do it with Xpath
I have no Idea why you are using JavaScript to click first Element, but unless click method provided by Selenium is not working I would suggest not to use JavaScript Click
Error suggests that element is not visible, it could be due to multiple reasons. You could wait using Explicit wait until element is visible as mentioned below. it might resolve your issue
Code
Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath("//*[#id='ctl00_Sitemap1_HyperLink1']"));
action.moveToElement(we).build().perform();
WebElement tmpElement= driver.findElement(By.xpath("//*[#id='ctl00_Sitemap1_HyperLink1']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
// I have no idea why you are clicking using JavaScript
js.executeScript("arguments[0].click();", tmpElement);
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement eventDashboardMenu = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[contains(text(),'Event Dashboard')]")));
eventDashboardMenu.click();