Exception org.openqa.selenium.ElementNotVisibleException: ..element not visible - java

Trying to select radio button but getting "ElementNotVisibleException:" exception.please refer below code
public class Automatecheckbox {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "G:/chromedriver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://www.facebook.com");
WebElement male_radio_button=driver.findElement(By.xpath(".//*[#id='u_0_c']"));
boolean status=male_radio_button.isDisplayed();
System.out.println("Male radio button is Displayed >>"+status);
boolean enabled_status=male_radio_button.isEnabled();
System.out.println("Male radio button is Enabled >>"+enabled_status);
boolean selected_status=male_radio_button.isSelected();
System.out.println("Male radio button is Selected >>"+selected_status);
Thread.sleep(1000);
male_radio_button.click();
boolean selected_status_new=male_radio_button.isSelected();
System.out.println("Male radio button is Selected >>"+selected_status_new);
}
}

Try to find element using this code :
WebElement male_radio_button = driver.findElement(By.xpath("//input[#id='u_0_c']"));
Hope it will work!!

Use the explicit wait (WebDriverWait) to let the WebElement get loaded before performing any execution on it. WebDriverWait object (in this case wait) is initialized just after invoking the ChromeDriver();
WebDriver driver = new ChromeDriver();
/* Initialize the WebDriverWait, with 30 seconds of wait time. */
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='u_0_c']")));
WebElement male_radio_button=driver.findElement(By.xpath(".//*[#id='u_0_c']"));

Use below code:
driver.findElement(By.xpath("//input[#value='2']")).click();

Related

How to do Resize using Java and Selenium?

How to do Resize using Java and Selenium?
Link
Code I have been trying:
public static void main(String[] args) throws InterruptedException
{
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
driver.get("https://jqueryui.com/resizable/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.switchTo().frame(0);
Thread.sleep(2000);
WebElement resize = driver.findElement(By.xpath("//div[#id = 'resizable']/div[3]"));
//WebElement resize = driver.findElement(By.id("resizable"));
new Actions(driver).dragAndDropBy(resize, 400, 400).perform();
}
the this is the below error:
> Exception in thread "main"
> org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: move
> target out of bounds
The code that you'd shared earlier, I have made a few changes in that, Please see the below code.
The web element is in iframe, therefore we will have to first switch to the iframe and then use action class dragAndDropBy with the following properties:
(WebElement source, int xOffset, int yOffset)
Code:
driver.manage().window().maximize();
//driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("https://jqueryui.com/resizable/");
Actions action = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe.demo-frame")));
WebElement resizeButton = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.ui-icon-gripsmall-diagonal-se")));
action.dragAndDropBy(resizeButton, 100, 50).build().perform();

Alert - Selenium

Not be able to click on 'OK' in Alert box in the last step. I also tried the control the pop up window
public class MyFirst {
public static void main(String[] args) throws NoAlertPresentException,InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\kunal.bhaskar\\Downloads\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseUrl = "https://www.goindigo.in";
driver.get(baseUrl);
driver.findElement(By.xpath("//*[#id=\"roundWay\"]/form/div[1]/ul/li[1]/input[1]")).clear();
driver.findElement(By.xpath("//*[#id=\"roundWay\"]/form/div[1]/ul/li[1]/input[1]")).sendKeys("Patna");
driver.findElement(By.xpath("//*[#id=\"roundWay\"]/form/div[1]/ul/li[2]/input[1]")).sendKeys("Bengaluru");
driver.findElement(By.xpath("//*[#id=\"roundWay\"]/form/div[1]/ul/li[3]/input")).click();
Select adult = new Select(driver.findElement(By.xpath("//*[#id=\"roundWay\"]/form/div[1]/ul/li[3]/div/div[1]/label[2]/select")));
adult.selectByVisibleText("2");
driver.findElement(By.xpath("//*[#id=\"depart-date\"]")).clear();
driver.findElement(By.xpath("//*[#id=\"depart-date\"]")).sendKeys("18 Oct 2017");
driver.findElement(By.xpath("//*[#id=\"return-date\"]")).clear();
driver.findElement(By.xpath("//*[#id=\"return-date\"]")).sendKeys("28 Oct 2017");
driver.findElement(By.xpath("//*[#id=\"roundWay\"]/form/div[1]/div/div/div/ul/li[1]/div/div[1]")).click();
driver.switchTo().alert().accept();
}
Actually, that's is not an alert. It's a modal box that's why you get the no alert present Exception.
code for modal box.
WebElement findElement = driver.findElement(By.xpath("//*[#id='globalModal']/div/div/div[3]/button"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click()", findElement);
write these lines in your code instead of driver.switchTo().alert().accept();.
it will work fine.
It is not an alert dear and you rty to close it with alert.accept() thats why gave exception
try this
WebElement alertbox= driver.findElement(By.xpath(" //*[#id='globalModal']/div/div/div[3]/button"));
alertbox.click();

Slider error always Selenium Webdriver

Use of everything done. No output only shows an error. Have tried all these ways but it only shows an error. Totally lost it. Nothing is moving forward from yesterday. Kindly look into it and please let me know.
public class task {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "g://geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.cheapoair.com/");
driver.manage().deleteAllCookies();
driver.findElement(By.xpath(".//*[#id='ember746']")).sendKeys("DFW");
driver.findElement(By.xpath(".//*[#id='ember751']")).sendKeys("JFK");
driver.findElement(By.xpath(".//*[#id='owFlight']")).click();
driver.findElement(By.xpath(".//*[#id='departCalendar_0']")).click();
driver.findElement(By.xpath(".//*
[#id='calendarCompId']/section/div/div[1]/ol/div[26]/li")).click();
driver.findElement(By.xpath(".//*[#id='ember751']")).sendKeys("JFK");
driver.findElement(By.xpath(".//*[#id='owFlight']")).click();
driver.findElement(By.xpath(".//*
[#id='ember730']/section/form/input")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//WebElement target = driver.findElement(By.xpath(".//*
[#id='DivDepart']/div/div/div[1]/div[2]"));
//WebElement source = driver.findElement(By.xpath(".//*
[#id='DivDepart']/div/div/div[1]/div[1]"));
//a.dragAndDrop(source, target).build().perform();
WebElement slider = driver.findElement(By.xpath(".//*
[#id='DivDepart']/div/div/div[1]/div[2]"));
//WebElement slider = driver.findElement(By.id("DivDepart"));
Actions a = new Actions(driver);
//a.dragAndDropBy(slider, 30, 0).build().perform();
//a.clickAndHold(slider).moveByOffset(30,
0).release(slider).build().perform();
//System.out.println("moved");
//JavascriptExecutor js = (JavascriptExecutor)driver;
org.openqa.selenium.interactions.Action dragAndDrop =
a.clickAndHold(slider).moveByOffset(40,0).release().build();
dragAndDrop.perform();
//js.executeScript("window.scrollBy(200,0)");
}
}
Note :-
In your code I can see clearly that, you have not selected depart date and arrival date and you are directly clicking on "Search Now" button. Validation messages are appearing if you directly click on search button without selecting depart and arrival date. So webdriver is not able to find the slider that your are interested in. Code you have written for slider is working fine. Do not change it.
public class task {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "D:/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.cheapoair.com/");
driver.manage().deleteAllCookies();
driver.findElement(By.xpath(".//*[#id='ember746']")).sendKeys("DFW");
driver.findElement(By.xpath(".//*[#id='ember751']")).sendKeys("JFK");
driver.findElement(By.xpath(".//*[#id='owFlight']")).click();
driver.findElement(By.xpath(".//*[#id='departCalendar_0']")).click();
driver.findElement(By.xpath(".//*[#id='calendarCompId']/section/div/div[1]/ol/div[26]/li")).click();
driver.findElement(By.xpath(".//*[#id='ember751']")).sendKeys("JFK");
driver.findElement(By.xpath(".//*[#id='owFlight']")).click();
//------- Corrections
driver.findElement(By.xpath("//*[#id='departCalendar_0']")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#id='calendarCompId']/section/div/div[1]/ol/div[27]/li")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#id='calendarCompId']/section/div/div[1]/ol/div[31]/li")).click();
// Corrections--------
driver.findElement(By.xpath(".//*[#id='ember730']/section/form/input")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement target = driver.findElement(By.xpath(".//*[#id='DivDepart']/div/div/div[1]/div[2]"));
WebElement source = driver.findElement(By.xpath(".//*[#id='DivDepart']/div/div/div[1]/div[1]"));
WebElement slider = driver.findElement(By.xpath(".//*[#id='DivDepart']/div/div/div[1]/div[2]"));
((JavascriptExecutor) driver).executeScript("scrollBy(0,500);");
Actions a = new Actions(driver);
org.openqa.selenium.interactions.Action dragAndDrop =
a.clickAndHold(slider).moveByOffset(40,0).release().build();
dragAndDrop.perform();
} }
Just Execute this code, I have executed this and working fine and as expected.
I just picked your code and executed once, After correction now it is working very fine. Your Slider code is perfect no need of change in it.
Issue was you had not written script for selecting dates[departure and arrival]
You can also try this code to move Slider :
Thread.sleep(5000);
Actions builder1 = new Actions(driver);
WebElement zero = driver.findElement(By.xpath(".//*[#id='DivDepart']/div/div/div[1]/div[2]"));
builder1.dragAndDropBy(zero, 1000, 0).perform();
xpath of slider square :- //*[#id='slider-range']/span[1]
Please let me know if it is working at your end.
After entering From/To & date details when user clicks 'Search' button. Then moves to next screen. From there I believe you would like to move the slider. Then try following code and you may have to alter wait based on application need.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Robot rob = new Robot();
rob.keyPress(KeyEvent.VK_PAGE_DOWN);
rob.keyRelease(KeyEvent.VK_PAGE_DOWN);

cant select the tab drag n sortabke in t he link http://demoqa.com/draggable/ (none of the tabs there)

I tried using all known element identifier to select the tabs in the drag n drop options in the link but constantly giving error object cant be located .
can anyone help me.
public class DragnDrop {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "D:\\Auto\\geckodriver-v0.11.1-win32\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://demoqa.com");
driver.findElement(By.xpath(".//*[#id='menu-item-140']/a")).click();
Actions builder = new Actions(driver);
WebElement know = driver.findElement(By.xpath(".//*[#id='tabs']/ul"));
builder.moveToElement(know, 10, 10 ).click().build().perform();
i tried to even use action to click at particular location none worked
As far as I can see, if it's the "Draggable + Sortable" tab you're trying to click, the below should allow that:
WebElement draggableTab = driver.findElement(By.id("ui-id-5"));
draggableTab.click();
I've tried the above (using C#) and it worked fine.
public class DragDrop {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver",
System.getProperty("user.dir") + "\\src\\Browser_Driver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://demoqa.com");
driver.findElement(By.xpath(".//*[#id='menu-item-140']/a")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(text(), 'Draggable + Sortable')]")));
driver.findElement(By.xpath("//a[contains(text(), 'Draggable + Sortable')]")).click();
// code works perfectly for demoqa site
Actions builder = new Actions(driver);
List<WebElement> list = driver.findElements(By.cssSelector("#sortablebox li"));
WebElement source = driver.findElement(By.id("draggablebox"));
WebElement dest1 = list.get(1);
WebElement dest2 = list.get(4);
builder.click(source).clickAndHold().moveToElement(dest1).moveByOffset(0, 10).release().build().perform();
Thread.sleep(2000);
builder.click(source).clickAndHold().moveToElement(dest2).moveByOffset(0, 10).release().build().perform();

How do I fill the email section using chrome driver

enter image description here
Can you help me to input text on the email box? The email box appears when I click on masuk button on the top, but I can't get to sendkeys on the email box.
H|ere is the url- www.tokopedia.com
And here is the code that does not work
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "E:\\Download\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.tokopedia.com/");
Thread.sleep(3000);
WebElement element = driver.findElement(By.id("login-ddl-link"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
driver.findElement(By.id("login-ddl-link")).click();
driver.switchTo().frame("iframe-accounts");
WebElement myEmail = driver.findElement(By.id("inputEmail"));
myEmail.sendKeys("tes213");
WebElement myPassword = driver.findElement(By.id("inputPassword"));
myPassword.sendKeys("tes123");
}
This is because authorization form located inside iframe element. You need to switch to that frame at first and then handle input fields:
...
driver.findElement(By.id("login-ddl-link")).click();
Thread.sleep(2000);
driver.switchTo().frame("iframe-accounts");
WebElement myEmail = driver.findElement(By.id("inputEmail"));
myEmail.sendKeys("tes123");
...
To switch back you might need to use
driver.switchTo().defaultContent();
P.S. You don't need to click on input field to sent text to it, so driver.findElement(By.id("inputEmail")).click(); is redundant line
use this code it worked just fine for me on Chrome.
public static void main(String [] ar) throws Exception {
System.setProperty("webdriver.chrome.driver", "E:\\Download\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.tokopedia.com/");
Thread.sleep(3000);
WebElement element = driver.findElement(By.xpath("//*[#id='login-ddl-link']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
driver.switchTo().frame("iframe-accounts");
WebElement myEmail = driver.findElement(By.id("inputEmail"));
myEmail.sendKeys("tes213");
WebElement myPassword = driver.findElement(By.id("inputPassword"));
myPassword.sendKeys("tes123");
driver.findElement(By.xpath(".//*[#id='global_login_btn']")).click();
}

Categories