Selenium Driver - How to click a value from a sub-dropdown - java

I'm sort of new to Web Automation and I'm having some trouble interacting with clicking a value from a sub-dropdown.
I'm able to select the span, click the "Apply Special" link to make the drop-down appear, but no matter what I do, I can't hit the specific "li". I can get all the text from the span, but I don't know how to actually make it so it clicks the specific special.
#FindBy(css = ".sub-dropdown")
protected List<WebElement> specialsTest;
#FindBy(id = "ctl00_ctl00_content_content_ucOrderWorkflow_upnlApplyDiscount")
protected Element specialItems;
public NewOrderPage addSpecificSpecialToOrder(String special) {
Reporter.log(String.format("Add special %s to order.", special), true);
//This clicks the Apply Special link
specialItems.waitUntilVisible().click();
//This prints the content of the span, just to make sure I'm hitting the right dropdown
String text = specialsDropDown.waitElementsReady().then().getText();
System.out.println("Dropdown getText " + text);
//This is my attempt to find the <li> text and click it, but it's not working :(
for (WebElement li : specialsTest){
System.out.println(li.getText());
if (li.getText().contains(special)) {
li.click();
break;
}
}
return this;
}
Any help, would be highly appreciate it.
Thank you in advance, and let me know if I need to add more info.

As I saw, the element which you are trying to click is an a tag ,so you need to use correct locator to point this element. We can use:
Link/ Partiallink locator
Xpath locator: .//a[contains(.,'Percentage Discount')]

Related

How use the sendKeys(Keys.TAB) + sendKeys("text") for contact us form

How to fill in the few fields in a column, with commands sendKeys(Keys.TAB) and sendKeys("text").
For Example:
We have 3 fields in Contact Us form at the bottom of the page:
Name
Email
Message
And Submit button.
I have scroll the page to the "Name" field and filled in the "Name", TAB and want to filled in the another text in the next fields (email) by using the sendKeys(Keys.TAB)
That's how I tried it:
WebElement element = driver.findElement(By.cssSelector("#contact > div > div.main > div.form-block"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
WebElement webElement = driver.findElement(By.xpath("//*[#id=\"edit-submitted-name\"]"));
webElement.sendKeys("Name");
webElement.sendKeys(Keys.TAB);
webElement.sendKeys("email");
webElement.sendKeys(Keys.TAB);
webElement.sendKeys("Message");
But it filling in all texts in the first field because webElement moving to first field ("Name").
I know, that I can use driver.FindElement(By.xpath(## next field ##)).sendKeys("text");
But I need exactly to use the TAB button for testing.
Maybe in Java exist the command, to type the text in the focused field? I can't find it.
You have to use Action class of Selenium and create a series of actions to use tab.
Also try all steps manually by clicking tab. sometime tab not work on webpages.

How to click button using selenium webdriver java, whose xpath is not constant

I am new to selenium webdriver in Java. I am using webdriver with Firefox Quantum (59.0, x64). The problem I am facing is that the website i am writing code for testing dose not have constant xpath. On every visit the button id changes. Even the classname(launchbutton) is same for all 4 Launch Buttons. So while writing code it initially worked & opened the first link, but for second link it again opened the first link as the classname is same for all 4 buttons. Please help me out to code to open other links too by pressing other launch button.
i used this(below) code but it worked partially. Please help me out as i want to launch using button of other subjects too. (Please refer screenshot)checkout screenshot1 here
checkout screenshot2 here
Also i am not able to switch between tabs. pls help.
thankyou
driver.findElement(By.className("launchbutton")).click();
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
driver.get("http://google.com");
//Grab all elements which have the className "launchbutton"
List<WebElement> elements = driver.findElements(By.className("launchbutton"));
//Iterate your list of matching elements and look at the innerText for the button you want
//When found, click it and break the for loop
foreach(WebElement ele in elements)
{
if(ele.getAttribute("innerText") == "The button text you want")
{
ele.Click();
break;
}
}
You can click the Launch button as below and please change the code based on the mentioned comments
//Specify the Expected Name in the below String
String subjectName="";
//Find the Table Element using any one of the Unique Locator
WebElement table=driver.findElement(By.id(""));
List<WebElement> rowElementList=table.findElements(By.xpath(".//tr"));
//I have assumed Launch button column in 5th.So, Specified as td[4].
//Please cross check the Launch button column and change the index accordingly
for(WebElement element:rowElementList){
if(element.findElement(By.xpath("./td")).getText().equalsIgnoreCase(subjectName)){
element.findElement(By.xpath("./td[4]/a")).click();
}
}

Selenium Automation unable to click the link

I am trying to automate some task for a website. In on page there is a option to "Move to next page" its not usual a link but when I click it takes me to the next page. The code is given below. I tried all possible ways to locate it by id, classname, xpath and css selector none of them are working. Please help me to find the way for cases with javascript:void(0). I tried the methods from some other stackoverflow questions asked by others...those are also not working.
WebElement skip = driver.findElements(By.cssSelector("a[href*='javascript:void[0]']"));
skip.click();
You can find the <a> and use filter on the text "Move to next page" to locate the element you're after, unless you have multiple elements with that text.
Also check if the id is unique, if there are more than one element with that id, it returns the first hit. You can use jQuery regex match to find them all, and find a way to locate that element.
EDIT: with the source code you provided:
::before "Move to next page" == $0
Ignore those $0 & ::before that are obviously copied from the developer console of the browser, it should be something like this:
Move to next page
So, in Selenium, you can locate it with:
String jsScript = "return $('a.someclassname:visible').filter(function () { return $(this).text() == 'Move to next page'; });";
Object object = ((JavascriptExecutor)this.driver).executeScript(jsScript, new Object[0]);
if (WebElement.class.isInstance(object))
{
return (WebElement) object;
}
if (List.class.isInstance(object))
{
return (WebElement) ((List)object).stream().filter(WebElement.class::isInstance).findFirst().orElse(null);
}
return null;
Either of these methods should work
WebElement element = driver.findElement(By.linkText("Move to next page"));
element.isEnabled();
element.click();
WebElement element = driver.findElement(By.partialLinkText("Move to next page"));
element.isEnabled();
element.click();
Use below xpath to click on-
driver.findElement(By.xpath("//a[#class='someclassname'][contains(text(),'Move to next page')]")).click();

how to choose a value in dropdown which is not in select tag ..?

I am using selenium-RC for my automation with java (Eclipse Kepler) . I am facing some issues with a selection of a option in a drop down list which is not in select tag it is in span.
I want to test a page where their is a drop-down list for selection of city name. the drop-down appears only when I give some value e.g for "Bangalore" I have to type "ban" so the drop-down appears and then I select the city Bangalore either with "mouse click" or either with "down-arrow and enter key" but when I run my selenium rc script it fails after typing "ban" the drop-down doesn't appears. i tried using both xpath and id in select command,click command. I am stuck over here please please someone help me to solve this issue.I think it is dynamic and based on JavaScript functions.One more ..The next two drop down are depended on the first drop-down(i.e the second and third drop-down are hidden and first drop-down is shown by_default.)
I am sending the link of page where i am trying to test script: https://pizzaonline.dominos.co.in
I tried using following commands:-
1:
w.click("//*[#id='homedeliveryform']/div[1]/span/a/span[1]");
w.click("//*[#id='ui-active-menuitem']");
2:
w.type("//*[#id='homedeliveryform']/div[1]/span/input","ban");
w.click("//*[#id='ui-active-menuitem']");
3:
w.select(" id=combobox", "value=BANGALORE");
4:
w.type(" id=combobox", "value=BANGALORE");
I've had a similar problem. This is how I solved it:
Find the arrow button in the combobox and click it, the dropdown list will appear.
The dropdown list doesn't really have anything to do with the combobox, it is a separate list wich is just displayed in a correct position so that it looks like it belongs to the combobox. The dropdown list is actually an <ul>-element, which doesn't seem to be very easily found. Anyway there are many <li>-elements with <a>-elements in them, and those hold the texts.
As i can see you are trying to select a webelement(Bangalore) from a dropdown which is not visible till the time you click on it. Here are the ways by which you are able to select a hidden web element.
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();
Hey thank you so much for the help. I solved this by
w.keyPressNative(String.valueOf(KeyEvent.VK_B));
w.keyPressNative(String.valueOf(KeyEvent.VK_A));
w.keyPressNative(String.valueOf(KeyEvent.VK_N));
w.keyPressNative("40"); // down arrow key
Thread.sleep(4000);
w.keyPressNative("10"); //Enter key
In the below scenario what I did is...(dropdwon is in the table)
clicked the cell
get all cell values from the table(probably the list will be configured as table or list .. here it is table)
traverse through all the cells.
click on the required value based on the cell value.
driver.findElement(By.id("ms__id3")).click();
WebElement table = driver.findElement(By.className("combo-list-table"));
allRows = table.findElements(By.tagName("tr"));
for (WebElement row : allRows)
{
cells = row.findElements(By.tagName("td"));
for (WebElement cell : cells)
{
if(cell.getAttribute("text").equalsIgnoreCase("Business"))
{
cell.click();
}
}
}
Hope this will help...:)

Can anyone tell me how to train Selenium WebDriver on this dropdown menu?

There is a dropdown menu of states on this website (see link below). This is the toughest one I have had to do. Can anyone help me figure out how to select an item from this menu? The problem is that the DOM doesn't change when the list gets populated and so I don't know how to select it.
US Post Office Address Search Page
#FindBy(css = "span.select-current-text") private WebElement state;
public void selectElementByString(WebElement field, String str ) {
state.click();
// menu appears but doesn't appear in HTML so how do I select?
}
This is how Selenium IDE recorded it:
selenium.click("css=span.select-current-text");
selenium.click("link=RI - Rhode Island");
I suspect the control is created with DOJO Toolkit as seen here .
This should work..
//Name of the state to select.
String stateName = "AL - Alabama";
//Open Url.
driver.get("https://tools.usps.com/go/ZipLookupAction!input.action");
//Click on state select box, which makes the list visible.
driver.findElement(By.className("select-current-text")).click();
//Select the state from the list.
driver.findElement(By.partialLinkText(stateName)).click();

Categories