Selenium - auto completion (prediction text) - java

I am new to selenium web driver.
When I try to auto-complete a particular text and select an option from the prediction text list as given below, it selects the appropriate option.
The problem is after the phrase gets populated in the text box, the prediction text list is displayed again, due to which it is unable to perform the next step.
I am using xpath to select the option from the prediction text list:
driver.findElement(By.xpath("//div[#class='mui-pt-bd']//li[2]")).click();
Please let me know what I can do regarding this.

Try adding some synchronization code to wait for the prediction list. After that call the click method. See http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp

First, make sure there are no trailing spaces for the prediction text that you chose by clicking from the list. If there are, it might trigger the prediction text list to open up again.
If this not the case, the only thing that I can think of is, clicking elsewhere within the same frame/form so that the prediction text list gets closed.
Another thought is that, after choosing one item, the usually expected behavior is for the drop-down to close. If that is not happening manually, then, you might want to make sure it is not a bug with the Application itself.

Related

Trying to add data to boxes without the xpath, a tricky situation [SELENIUM & JAVA]

I am working on this grid, using SELENIUM & JAVA and chromedriver.
Take a look at this and it's behaviour (it is a .gif):
When i need to add a new row to that grid i have to click on
"ADD OPTION" and then a new row is inserted
The problem is that i do not understand how to pass a collection of values, i want to achieve this:
I want to pass a collection and my program should place them in the grid (2 values each row) without getting the xpath of every single box. I need to make it more efficient.
Example: i have this collection:
["fdfdfddf","989"; "RERE","6655"; "HEHE","554"; "TTER","89"]
I want my program to place them in the GRID.
Desired result:
The problem is that in my code i do need to know the xpath of each "box" of the grid in order to insert data.
This is my code to add data to the grid:
driver.findElement(By.id("add_new_option_button")).click(); //it clicks on "Add Option" button
driver.findElement(By.xpath("//*[#id=\"manage-options-panel\"]/table/tbody/tr[40]/td[3]/input")).sendKeys("fdfdfddf");
driver.findElement(By.xpath("//*[#id=\"manage-options-panel\"]/table/tbody/tr[40]/td[5]/input")).sendKeys("989");
driver.findElement(By.id("add_new_option_button")).click();
How can i fill in the boxes without knowing the xpath of every box?
I don't want to click on every box of each row to get the xpath, i need to find another fast solution.
This is how i get the xpath of every box:
You can solve this by using xpath like.
Xpath=//*[#type='text']//following::input
Here First target a element which is just before your input box then when you add any new box then the xpath will like
Xpath=//*[#type='text']//following::input[1]
Xpath=//*[#type='text']//following::input[2]
Xpath=//*[#type='text']//following::input[3]
Now you can run a loop or otherway and input value inside the box without taking different xpath

How to click on file-attachment icon using selenium (with java)?

I am creating automation that automatically sends messages through LinkedIn. The code is working fine, but as per our current requirement, I want to click on the attachment button so as to pass the attachment also with the text message. There are a few points that I have already tried:
Unable to fetch the element ID as its dynamic most of the time.
Don't want to use offsets (x,y) because once anyone changes the size of the window offsets changes.
Unable to use XPath.
Then I checked for another tool "UIVision" which makes a solution to capture that particular area (save it in a .png file) from the page where we want to click and during runtime it clicks there. So I tried searching image comparison API for java, tried with ASHOT and Sikulix too but none worked for me.
Anyone can help me with this?
When you say your unable to use xpath will you please elaborate?
The button has an id - it's attachment-trigger-ember1079.
If you're concerned about the latter part being dynamic, the attachment button also has a good title.
You can try using xpath string:
//button[contains(#title,'Attach a file')]
For me, on my linked in, this returns a unique hit on the expected attachment object:
If these don't work, there's also the option of using javascript to action the upload.
For more information on handling the upload dialog (after your identify the object) have a look here

Accessing an element with unknown id but known path

I am working on a testing program that operates with very little information. In this particular case, my program doesn't know the ID of elements in the page before it runs, because the Javascript on the page dynamically assigns those at run time. The only constants I have is the structure and the text I'm looking for. I'm including a screenshot of one example of the DOM being generated. In this case I know that I want to access the button with text apply that is displayed next to the label with the text "To Location:" Is there a way to use xpath manipulate their relationship and ensure that I'm accessing the right element. I can't just access the apply button because there are 6 apply buttons on the page with dynamically generated IDs. The label's next to them are different so I'm trying to use that and manipulate the path's from there. Help?
This is possible. If you provide the entire html code I could provide a better xpath. But for what you pasted, here's a simple one that might work:
//td[div//label[text()='To Location:']]/following-sibling::td[1]//button[text()='Apply']
There's a slightly longer winded way but thats generating a list of elements by class and clicking the one with the right text
`var elements = driver.FindElements(By.Class("text-pb"));
foreach(var element in elements)
{
if(element.Text.Equals("Searched Text"))
{
element.click();
}
}`
that might work thats if you want to click the button.
i use these sort of things on the pages works site generates so it should do what your after.

Selenium Webdriver (Java) , Need to send "Space" keypress to the website as whole

My problem is as follows.
I am attempting to automate part of a test suite for a website I work in, and while most of it went well, I'm at the point where the developers added a lightbox to confirm the next action.
Using firebug I found out an xpath I could use to click the button I need to proceed, but sadly it isn't working.
After some manual attempts, I figured that pressing the "Space" key, I can proceed.
The problem is that any sort of try using "driver.findElement" be it by xpath, or link text, fails with a "No such element" error in console.
So I'm trying to just send the keypress of Space , without using find element.
To be clear, I want to emulate someone just pressing space without clicking or selecting anything beforehand.
I tried with driver.Keyboard... but "Keyboard" isn't recognized, so I'm at a loss of how to send this keypress without using driver.findElement.
The piece of code giving me problems is:
driver.findElement(By.xpath("//div[4]/div[3]/div/button")).click();
Any help would be appreciated.
Thank you and have a great day!
If you receive NoSuchElementException, but you know that the element is there, then it seems that the element get loaded into the DOM later (with ajax?), than the page get loaded.
In this case you should use Implicit or Explicit Wait to wait until an element present, or an element is visible, etc...
If it still doesn't work, and you want to try the Space Key thing, then you can perform it on any element, for example on the <body> tag:
WebElement body = driver.findElement(By.tagName("body"));
body.sendKeys(Keys.SPACE);
Hope it helps.
try this:
Actions action = new Actions(Driver);
action.SendKeys(Keys.Space).Build().Perform();

Selenium Webdriver - unable to select an option from the list box

I am trying to select an option Pune(PNQ) from "Leaving from" list box of this page
http://book.spicejet.com/
driver.get("http://book.spicejet.com");
Thread.sleep(50000);
Select S = new Select(driver.findElement(By.id("ControlGroupSearchView_AvailabilitySearchInputSearchVieworiginStation1")));
S.selectByValue("PNQ");
But I am receiving this error:
org.openqa.selenium.ElementNotVisibleException
I am new to Selenium. Please help.
Straight from the Selenium source -
/**
* Thrown to indicate that although an element is present on the DOM, it is not visible, and so is
* not able to be interacted with.
*/
public class ElementNotVisibleException...
As it says, the element is there on the DOM, but not visible to operate with. If there a preemptive action you have to take before that element exists, then do that.
An example would be Google image searches. When you click on an image, a black box appears with the picture. That element is always there, but you have to click on an image to make it appear.
Sounds like the same thing is happening with your select box.
Edit
I took the liberty of further looking at your particular issue.. it looks like that site hides that <select> tag because it's filled in by some jQuery stuff.
Instead of using the select tag and selecting by value, do,
driver.findElement(By.id("ControlGroupSearchView_AvailabilitySearchInputSearchVieworiginStation1_CTXT").sendKeys("PNQ");
driver.findElement(By.cssSelector("a[value='PNQ']").click();
Hope this helps.

Categories

Resources