Mouse Action is not not working at runtime - java

if i debug same code then its working fine but when run this code then Mouse Action is not not working.
code are following -
public static void main(String[] args) {
FirefoxDriver driver = new FirefoxDriver();
driver.get("url");
driver.findElementByXPath("xpath").click();
driver.findElementByXPath("xpath").sendKeys("gg");
driver.findElementByXPath("xpath").click();
boolean saleIdVisible =driver.findElementByXPath("path").isEnabled();
if(saleIdVisible==true){
Actions mouseaction=new Actions(driver);
WebElement payment_lk1 = driver.findElement(By.xpath("path"));
mouseaction.moveToElement(payment_lk1).build().perform();
mouseaction.click(payment_lk1).build().perform();
System.out.println("order id is not found ");
}else{
System.out.println("order id is found ");
}
driver.findElementByXPath("path").click();
driver.findElementByXPath("path").click();
driver.findElementByXPath("path").clear();
driver.findElementByXPath("path").sendKeys("95032");
driver.findElementByXPath("path").click();
}

You don't need to do 2 steps for that kind of action.
mouseaction.click(payment_lk1).build().perform();
instead of
mouseaction.moveToElement(payment_lk1).build().perform();
mouseaction.click(payment_lk1).build().perform();
Can you explain more about your : payment_lk1. Is this a link ? Button ? ...
PS : Take care there
boolean saleIdVisible =driver.findElementByXPath("path").isEnabled();
because a button can be enabled but not visible. ;)

Related

How to switch focus with Selenium to a new pop-up Window with the SAME handle?

I am trying to change my driver's focus to the new window that pops up as a result of clicking a link. The only problem is that both the parent window and the pop-up window have the same handle - for example - the title of both is " <Company Name - Abbreviation 1>
How would I use Selenium JAVA to get focus on the pop-up window a different way than what I have used?
`
String originalWindow = driver.getWindowHandle();
assert driver.getWindowHandles().size() == 1; driver.findElement(By.xpath("xpath goes here")).click(); // Training
// Loop through until we find a new window handle
for (String windowHandle : driver.getWindowHandles()) {
if (!originalWindow.contentEquals(windowHandle)) {
driver.switchTo().window(windowHandle);
driver.manage().window().maximize();
}
}
`
Thanks, Everyone!
`
String originalWindow = driver.getWindowHandle();
assert driver.getWindowHandles().size() == 1;
driver.findElement(By.xpath("xpath goes here")).click(); // Training
Thread.sleep(1000);
// Loop through until we find a new window handle
for (String windowHandle : driver.getWindowHandles()) {
if (!originalWindow.contentEquals(windowHandle)) {
driver.switchTo().window(windowHandle);
driver.manage().window().maximize();
}
}
`
I tried the above code, but both windows sadly have the same title. I want to switch focus to the new window so I can then interact with the new one.

how to select the auto popup hotel names in a hotel booking site in Selenium

I am working on automating the following hotel booking site. I need to select the auto popup hotel name once I type the hotel in the first search box...I don't know how to do this.
I have navigated through the link and clicked Demo, then clicked the first link that appeared on the page.
I tried to click on the first search box and I need to enter a hotel from the auto popup list...I don't know how to do this because this has no PAC-item...
https://www.phptravels.net/home
public class Question1 {
WebDriver Driver = null;
WebDriverWait wait = null;
String url = "https://phptravels.com/demo/";
#BeforeTest
public void beforeTest() {
System.setProperty("webdriver.chrome.driver","src\\test\\resources\\drivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("excludeSwitches",Arrays.asList("disable-popup-blocking"));
options.addArguments("--disable-popup-blocking");
Driver = new ChromeDriver(options);
Driver.manage().window().maximize();
Driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
wait = new WebDriverWait(Driver, 25);
String winHandle = Driver.getWindowHandle();
//Driver.switchTo().window(winHandle);
//new WebDriverWait(Driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title='webpush-onsite']")));
//new WebDriverWait(Driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#deny.button.close"))).click();
}
#Test
public void f() {
Driver.get(url);
System.out.println("*****In the main page*****");
String xpathDemo = "//*[#id=\"mega-nav-navigation\"]/div/ul[1]/li[2]/a";
Driver.findElement(By.xpath(xpathDemo)).click();
String Title = "PHPTRAVELS | Travel Technology Partner";
/*try {
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*[#id=\"PopupSignupForm_0\"]/div[2]/div[1]")));
Driver.findElement(By.xpath("//*[#id=\"PopupSignupForm_0\"]/div[2]/div[1]")).click();
} catch(Exception e) {
System.out.println("No popup..."+ e.getMessage());
}
*/
String username = Driver.findElement(By.xpath("//*[#id=\"Main\"]/section[2]/div/div/div[2]/div/div/div[2]/div[2]/div/div[3]/div[2]/div")).getAttribute("innerText");
username = username.substring(6) ;
String password = username.substring(30);
System.out.println("Username text :"+username + "\npassword is:"+password);
Driver.findElement(By.xpath("//*[#id=\"Main\"]/section[2]/div/div/div[2]/div/div/div[2]/div[2]/div/div[1]/div/a")).click();
utils.HelperFunctions2.switchToWindow(Driver, Title);
Driver.findElement(By.xpath("//*[#id=\"s2id_autogen16\"]")).click();
Driver.findElement(By.xpath("/html/body/div[7]/ul")).click();
}
#AfterTest
public void afterTest() {
Driver.quit();
}
}
Below xpath is result of 1st hotel, changing the index it will intreact with rest of the elements.
After filing text to the hotel text box.
give Thread.sleep(2000);
use below xpath. I hope it will work
(.//ul[#class='select2-results']/following::div[#class='select2-result-label'])[2]
I have already built bots, auto image pickers and more and I have never used the By.xpath method. Classname is much easier to use!
public void f(){
Driver.get(url);
String getInputFocusId = "select2-input";
Driver.findElement(By.className(getInputFocusId).click();
//now focused!
String text = Driver.findElement(By.className("select2-match").getText();
//text is the first default search match of the list.
}
There is also a more complicated way.
In Selenium you can sendKeys to an Element.
If the input element is focused the first match is also focused.
By clicking on Enter the focused match will be searched and displayed in the input element.
Finally you have to read the data from the input Element!
public void f(){
Driver.get(url);
String getInputFocusId = "select2-input";
WebElement element = Driver.findElement(By.className(getInputFocusId);
element.click();
//element.sendKeys(Key.DOWN);
element.sendKeys(Key.ENTER);
String text = element.getText();
//this is the text of the first search match
//if you want to get the second or third just repeat sending the DOWN key.
}
IMPORTANT: Make sure to run each line delayed (200ms is a good time). This helps you finding errors... For example in the Instagram Auth Process I delayed a lot of lines, and it finally worked!
I hope my answer helps you!!!

Selenium WebDriver running in background

I'd like to have my selenium webdriver running in background while doing something else but each time I switch from window where test is executing it fails.
It seems that WebDriver doesn't remember handler for window where started tests - is it ok behaviour ? What is solution then ?
For running Selenium WebDriver in background you need to use headless webdriver for that you can use following code
public static void main(String[] args) {
// Declaring and initialising the HtmlUnitWebDriver
HtmlUnitDriver unitDriver = new HtmlUnitDriver();
// open google.com webpage
unitDriver.get("http://google.com");
System.out.println("Title of the page is -> " + unitDriver.getTitle());
// find the search edit box on the google page
WebElement searchBox = unitDriver.findElement(By.name("q"));
// type in Selenium
searchBox.sendKeys("Selenium");
// find the search button
WebElement button = unitDriver.findElement(By.name("gbqfba"));
// Click the button
button.click();
System.out.println("Title of the page is -> " + unitDriver.getTitle());
}

Multi select drop down

I am writing code for multi select using selenium(java) where i need the following task to be performed :
select multiple options in drop down
to click on button first selected which will print the first selected option from drop down.
to click on button get all selected which will print the all selected options in order .
I have this code which returns me undefined as a result for 2nd task.
public class MultipleSlectList {
public static WebDriver driver ;
#BeforeTest
public void startbrowser () throws Exception {
System.out.println("launching browser");
System.setProperty("webdriver.gecko.driver", "H:\\Selenium3\\geckodriver-v0.19.1-win32\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://www.seleniumeasy.com/test/basic-select-dropdown-demo.html");
}
#Test
public void selectlist () throws Exception {
WebElement ele1 = driver.findElement(By.id("multi-select"));
Select se= new Select(ele1);
se.selectByValue("New Jersey");
Thread.sleep(2000);
se.selectByValue("Texas");
Thread.sleep(2000);
se.selectByValue("Florida");
Thread.sleep(2000);
//Thread.sleep(10000);
WebElement btn1= driver.findElement(By.id("printMe"));
btn1.click(); // it is supposed to return New Jersy
WebElement firstOption = se.getFirstSelectedOption();
System.out.println("The First selected option is::" +firstOption.getText());
List <WebElement> oSize = se.getAllSelectedOptions();
int iListSize = oSize.size();
// Setting up the loop to print all the options
for (int i = 0; i < iListSize; i++)
{
// Storing the value of the option
String sValue = oSize.get(i).getText();
// Printing the stored value
System.out.println(sValue);
}
}
}
Please help me to proceed further.
I have tried with jquery as well but no luck .The result is same as "undefined" in both case .
Thanks !
Steps to select all dropdown options:
Find the SELECT WebElement by webdriver.
Create Select class which is used to operate dropdown list.
Get all options list in the dropdown list.
Loop the options list, get each option value and use
Select.selectByValue(optionValue) to select it.
Then all the dropdown option has been selected.
Below article has the code example to implement above scenario.
http://www.dev2qa.com/select-dropdown-list-selenium-webdriver/

how to select the drop down box in selenium web driver

Select se = new Select(driver.findElement(By.xpath(".//*[#id='33629']/div/div[1]/div[2]/div[1]/select")));
se.selectByIndex(7);
driver.findElement(By.xpath(".//*[#id='33629']/div/div[1]/div[2]/div[1]/select/option[8]")).click();
Above code doesn't work,please help
Error returned:
Exception in thread "main" org.openqa.selenium.NoSuchWindowException: no such window: target window already closed from unknown error: web view not found
org.openqa.selenium.NoSuchWindowException: no such window
Means the browser is close when you are trying to interact with it. Remove driver.close() from your code and put it only after you have finished all you interactions with the browser.
Edit
If you need to return to parent window after closing child window use driver.switchTo() again
// get parent window ID
String parentHandle = driver.getWindowHandle();
// switch to the new window
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(parentHandle))
{
driver.switchTo().window(handle);
}
}
//do something with the new window
// switch back to the old window
driver.close();
driver.switchTo().window(parentHandle);
windowIdbefore = driver.getWindowHandle();
System.out.println(windowIdbefore);
Set<String> windowid = driver.getWindowHandles();
for (String string : windowid) {
System.out.println(string);
driver.switchTo().window(string);
// enter code here
}
WebDriver driver=new FirefoxDriver();
Select s=new Select(driver.findElement(By.xpath("xpathExpression")));
s.selectByVisibleText("text");
s.selectByValue("value");
s.selectByIndex(1);
as i see here the dropdown box is present in div tag. i think with your code dropdown has been located but you are not able to select the value present in dropdown. Then follow below code
WebDriverWait wait = new WebDriverWait(d, 10);
Actions builder = new Actions(d);
WebElement selectvalue = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("your drop down xpath value")));
builder.mouse.mouseMove(((Locatable)selectvalue).coordinates);
selectvalue.click();
WebElement option = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("locator value of dropdown value(your dropdown value)")));
builder.mouse.mouseMove(((Locatable)option).coordinates);
option.click();
System.out.println("dropdown value slected...");

Categories

Resources