Not able to select element from drop down list - java

I am facing a problem that i am not able to select element from drop down list to proceed further.
The URL for reference site is "http://www.rechargeitnow.com/needrecharge.jspx"
I tried the code below but didn't got success.
//WebDriverWait wait = new WebDriverWait(driver, 10);
//WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
WebDriverWait wait= new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("opId_div")));
//driver.findElement(By.cssSelector("select[id='operatorid']")).sendKeys("Airtel");;
//driver.findElement(By.linkText("mobile")).sendKeys("Airtel");
//driver.findElement(By.xpath("//*[#id='oprauto']")).click();
//driver.findElement(By.xpath("/html/body/div[2]/div/div[6]/div/div/div/div/div[2]/div/div/div/div/div[2]/select/option[5]")).findElement(By.name("Airtel"));
//operator.selectByIndex(1);
//driver.findElement(By.xpath("//*[#id='oprauto']")).sendKeys("Airtel");
driver.findElement(By.xpath("/html/body/div[2]/div/div[6]/div/div[3]/ul/li[3]/img")).click();
//Select operator=new Select(driver.findElement(By.id("operatorid")));
//operator.getOptions();
//operator.selectByVisibleText("Airtel");
driver.findElement(By.tagName(" mobile no. ")).sendKeys("9001785845");
driver.findElement(By.id("transSubscriptionNoID")).sendKeys("9001457868");
//driver.findElement(By.cssSelector("img[id='btn']")).submit();
//driver.findElement(By.id("btn")).submit();
driver.findElement(By.xpath("//*[#id='btn']"));

I am not familiar with Java but I was able to easily accomplish this in Ruby with the following code.
$driver.find_element(:xpath, ".//*[#id='input_dropdown']/div[1]/img").click
$driver.find_element(:link, "T24").click
You can replace "T24" with any of the other options available on the dropdown.
Hopefully this answers half of your question at least and someone will be able to translate this into Java.

So the combo drop down is not a select box, its an unordered list, wrapped around an input. You would need to click on the drop down icon and then click the element you want. Here is some dirty code that WORKS :). It selects "Idea" from the drop down.
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 300);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.rechargeitnow.com/needrecharge.jspx");
WebElement dropDownArrow = driver.findElement(By
.id("input_dropdown"));
dropDownArrow.click();
WebElement option = wait.until(ExpectedConditions
.elementToBeClickable(By.linkText("Idea")));
option.click();

The following code works. This is not what I wanted to provide you with as a code. But works none the less. You can replace Airtel with any other text present in the drop down.
driver.findElement(By.id("oprauto")).sendKeys("Airtel");
driver.findElement(By.xpath("//ul/li/a/strong[text() = 'Airtel']")).click();

Related

Get Text of Success alert message in Ant Design and verify it using Selenium

In one of my Projects, I have to verify the success alert text using Selenium. The UI is coded in react JS following Ant design. For verification please follow this link 'https://ant.design/components/message/' and click on Display Normal message. The message is shown above at the top of the page but I am not able to get the text from that using Selenium Webdriver coding in Core Java. Please help.
I tried this code:
driver.findElement(By.cssSelector(".ant-btn-primary")).click();
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement successmessage = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("ant-message-custom-content-ant-message-success")));
successmessage.getText();
To extract the text This is a normal message you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
cssSelector:
driver.get("https://ant.design/components/message/");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.ant-btn.ant-btn-primary"))).click();
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.ant-message"))).getText());
xpath:
driver.get("https://ant.design/components/message/");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='ant-btn ant-btn-primary']"))).click();
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='ant-message']"))).getText());
Console Output:
This is a normal message
I'm going to suggest:
driver.findElement(By.cssSelector(".ant-btn-primary")).click();
WebDriverWait wait = new WebDriverWait(driver, 15, 100);
String successMessage = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".ant-message-info > span"))).getText();
The main problem that you have is that the message doesn't last for very long, so you need to get the data out of it quickly before it is destroyed. As a result I have inlined the getText() call so that it's called as soon as the element is found.
I suspect part of your problem is getting an accurate locator. You can make it easier by opening up the chrome dev console, selecting the parent element where the notice message is created and then right clicking on that element and selecting break on -> subtree modifications. This will allow you to look at the intermediate state of the DOM by pausing JavaScript execution one the message is created. This makes it a lot easier to work out what is going on and find relevant locators.
driver.findElement(By.cssSelector(".ant-btn-primary")).click();
WebElement alertElmt = driver.findElement(By.xpath("//*[#class='ant-message']"));
for(int i=1; i<10; i++) {
Thread.sleep(500);
String getText = alertElmt.getText();
if(!getText.equals("")) {
System.out.println(getText);
break;
}
}
//or with webdriverwait
driver.findElement(By.cssSelector(".ant-btn-primary")).click();
WebElement alert = new WebDriverWait(driver,20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class='ant-message']")));
System.out.println(alert.getText());

How to clear the default values in the username textfield and enter my value in selenium webdriver

I have tried the following code but it is throwing the exception (ElementNotVisibleException)
FirefoxDriver dr = new FirefoxDriver();
dr.get("http://54.169.235.143/book.html?v=0.03");
System.out.println("First Testcase");
System.out.println(dr.findElement(By.id("user_name")));
dr.findElement(By.id("user_name"));
dr.findElement(By.id("user_name")).click();
dr.findElement(By.id("user_name")).getAttribute("user_name");
dr.findElement(By.id("user_name")).clear();
dr.findElement(By.id("user_name")).sendKeys("student100");
What am I doing wrong and how to fix it?
Actually your page taking time to load so web driver need wait until element gets visible , Below code will solve your issue :
WebDriverWait wait= new WebDriverWait(dr,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("user_name")));
dr.findElement(By.id("user_name")).clear();
dr.findElement(By.id("user_name")).sendKeys("test");
wait= new WebDriverWait(dr,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("pass_word")));
dr.findElement(By.id("pass_word")).clear();
dr.findElement(By.id("pass_word")).sendKeys("test");
I have just added wait for elements.
n software testing services this can be achieved by many ways some of the options are displayed above remaining are as follow.
Using java script
driver.executeScript("document.getElementByXpath('element').setAttribute('value', 'abc')");
Using action class Actions actions = new Actions(driver);
actions.click(driver.findElement(element) .keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).sendKeys(Keys.BACK_SPACE).build().perform());

Selenium click not always working

I have some tests which click on a tab, however the click is not always performed.
The xpath is correct as most of the times the test works
It is not a timing issue as I ve used thread.sleep() and other methods to ensure that the element is visible before clicking
The test believes that it is performing the click as it is not throwing an ElementNotFoundException or any other exceptions when 'performing' the click. The test fails later on after the click since the tab content would not have changed.
Further Info
I am using Selenium 2.44.0 to implement tests in Java which run on Chrome 44.0.2403.107 m.
Is there something else that I can do or could this be an issue with selenium?
There are several things you can try:
an Explicit elementToBeClickable Wait:
WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("myid")));
button.click()
move to element before making a click:
Actions actions = new Actions(driver);
actions.moveToElement(button).click().build().perform();
make the click via javascript:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", button);
you can go with linkText if the tab name contains any unique string. And make sure your tab is not dynamic. It should be visible in source code(manual source code(ctrl+u)).
The following method work for me
WebElement button = SeleniumTools.findVisibleElement(By.cssSelector("#cssid"));
Actions actions = new Actions(driver);
actions.moveToElement(button).click().build().perform();
I have a similar problem. Tried all solutions from the top answer. Sometimes they work, sometimes don't.
But running code in an infinite loop works always.
For example, we need to click on element-two which is not visible until element-one is clicked.
WebDriverWait wait = new WebDriverWait(webDriver, 10);
while (true){
try {
WebElement elementOne =
wait.until(ExpectedConditions.elementToBeClickable(By.id("element-one")));
elementOne.click();
WebElement elementTwo =
wait.until(ExpectedConditions.elementToBeClickable(By.id("element-two")));
elementTwo.click();
break;
} catch (Exception e){
//log
}
}
I have a similar problem. Here is my solution:
table_button = driver.find_element(By.XPATH, insert your xpath)
try:
WebDriverWait(driver, 15).until(EC.element_to_be_clickable(table_button)).click()
except WebDriverException as e:
print('failed')
print(e)
Through code above, you can find the error message if your button is not clickable.
For example, my error message is 'nosuchelement' and 'clcik is not clickable', then I got back to check the table_button.accessible_name, found it print a 'null' value, so that means my XPATH is incorrect.

Mouse clickAndHold() not working properly on Firefox Using Selenium Webdriver

I am using selenium webdriver client 2.39 and Firefox 26.
Mouse click and hold event does not work properly. My code is like
WebDriver driver=new FirefoxDriver();
driver.get("http://startingwithseleniumwebdriver.blogspot.com/2013/12/frmset1.html");
WebElement multiSelectDropDown=driver.findElement(By.name("multiselectdropdown"));
List<WebElement> dropdownlists = multiSelectDropDown.findElements(By.tagName("option"));
Actions builder=new Actions(driver);
builder.clickAndHold(dropdownlists.get(0)).
clickAndHold(dropdownlists.get(6)).click().build();
This code does not give any error but select only one element.
I can bypass this issue using other way but I want to know whay it is not working.
I face the same problem but it select the element from start to last and give some Error like
Cannot perform native interaction: Could not get node for element - cannot interact
I got the solution by this way you can do this for your problem
builder.clickAndHold(dropdownlists.get(0)).moveToElement(dropdownlists.get(6)).release().build().perform();
If you want to select multiple option from your list try this (it will select first 3 elements):
List<WebElement> elements = driver.findElements(By.xpath("//select[#name='multiselectdropdown']/option"));
for(int i = 0; i < 3; i++) {
new Actions(driver).keyDown(Keys.CONTROL).click(elements.get(i)).keyUp(Keys.CONTROL).perform();
}
ButtonUp (or release()) should be the next button-action following a ButtonDown (or clickAndHold()) button-action (see Appium notes for ButtonDown documentation). Your code performs two consecutive clickAndHolds() followed by a click() without performing a release(). It should be something like:
WebDriver driver=new FirefoxDriver();
driver.get("http://startingwithseleniumwebdriver.blogspot.com/2013/12/frmset1.html");
WebElement multiSelectDropDown=driver.findElement(By.name("multiselectdropdown"));
List<WebElement> dropdownlists = multiSelectDropDown.findElements(By.tagName("option"));
Actions builder=new Actions(driver);
builder.clickAndHold(dropdownlists.get(0)).moveTo(dropdownlists.get(6)).release().build();
While the linked documentation is not for Selenium, Appium is built on top of Selenium.

Test autocomplete with Selenium webdriver

I have a text box in which when I type one letter say 's' , it displays a list of results ( like google search) .
I am using latest selenium webdriver with java.
I have tried
sendKeys("s"),
JavascriptLibrary jsLib = new JavascriptLibrary();
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onkeyup");
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onblur");
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onclick");
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onmouseup");
driver.findElement(By.id("assetTitle")).sendKeys(Keys.ENTER);
None of these work even after adding wait after each of the steps.
Any suggestions?
Thanks.
Update :-
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement query = driver.findElement(By.name("q"));
query.sendKeys("s");
driver.findElement(By.xpath("//table[#class='gssb_m']/tbody/tr/td/div/table/tbody/tr/td/span")).click();
driver.findElement(By.name("btnG")).click();
Update 2 : -
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kayak.com/");
WebElement query = driver.findElement(By.name("destination"));
query.sendKeys("s");
Update 3 :-
I tried with Selenium 1 and the fireevent method works by passing parameter as 'keydown'. This should be a temporary workaround for now.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kayak.com/");
DefaultSelenium sel = new WebDriverBackedSelenium(driver,"http://www.kayak.com/");
sel.type("//input[#id='destination']", "s");
sel.fireEvent("//input[#id='destination']", "keydown");
I found a workaround about this. My problem was:
Selenium inputted 'Mandaluyong' to an auto-suggest location field
The auto-suggest field appears together with the matched option
Then selenium left the auto-suggest drop-down open not selecting the matched option.
What I did was:
driver.findElement(By.name("fromLocation")).sendKeys("Mandaluyong");
driver.findElement(By.name("fromLocation")).sendKeys(Keys.TAB);
This is because on a manual test, when I try to press TAB key, two things were done by the system:
Picks the matched option from the auto-suggest drop-down
Closes the auto-suggest drop-down
I believe you are testing auto-suggest here (not auto-complete)
Steps I follow -
Enter something in the input field
Click on the suggestion you want to choose. (You can find the xpath using some tools like Firebug with Firepath, Chrome, etc.)
Verify the text in the input field is same as expected.
This should be a temporary workaround for now.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kayak.com/");
DefaultSelenium sel = new WebDriverBackedSelenium(driver,"http://www.kayak.com/");
sel.type("//input[#id='destination']", "s");
sel.fireEvent("//input[#id='destination']", "keydown");

Categories