Unable to identify and click on the "Next" button in date field - java

I am trying to automate the flight booking of http://www.mercurytravels.co.in using Selenium+java.
The issue that I am facing is that I'm unable to click on the ">" (next button) of the "Date of Return" date field in the Book Flights Online page.
Getting error message that --> Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable
The code that I've written is:
driver.findElement(By.cssSelector("input[name='returnDate']")).click();
//Retrieving current year value
String currentReturnMonthYear = driver.findElement(By.xpath("//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[2]")).getAttribute("innerHTML");
driver.findElement(By.xpath("//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[3]")).click();
Request any inputs on issue that I'm facing?

org.openqa.selenium.ElementNotInteractableException Indicates that a click could not be properly executed because the target element was obscured in some way. ElementNotInteractableException is Thrown to indicate that although an element is present on the DOM, it is not in a state that can be interacted with.
Using Explicit wait:
driver.findElement(By.cssSelector("input[name='returnDate']")).click();
WebElement next = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(
"//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[3]")));
next.click();
Using Actions:
driver.findElement(By.cssSelector("input[name='returnDate']")).click();
WebElement next = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(
"//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[3]")));
Actions action = new Actions(driver);
action.moveToElement(next).click().build().perform();
Using JavascriptExecutor:
driver.findElement(By.cssSelector("input[name='returnDate']")).click();
WebElement next = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(
"//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[3]")));
JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
javascriptExecutor.executeScript("arguments[0].click();", next);

Related

I can't click on an element

the page is fully loaded and the element is located, but for some reason it can't be clicked, I can't understand why.
My test:
#Test
public void test(){
chromeDriver.get("https://alfabank.ru/currency/");
WebDriverWait wait = new WebDriverWait(chromeDriver, 8);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]")));
WebElement clicking = chromeDriver.findElement(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]"));
clicking.click();
}
Full xpatch it doesn't work either:
WebElement clicking = chromeDriver.findElement(By.xpath("/html/body/div[1]/div/div[6]/div/div/div/div/div[1]/div[1]/button[2]"));
Exception:
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element is not clickable at point (539, 1199)
None of the existing answer really explain what is their code is about.
Even if you launch the screen in full screen, USD is not in Selenium view port.
Also JS is only recommended when nothing works.
I am doing with Selenium class, actions, please see below :
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("https://alfabank.ru/currency/");
boolean b = wait.until(ExpectedConditions.titleIs("Курсы валют — «Альфа-Банк»"));
if (b) {
new Actions(driver).moveToElement(wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[data-test-id='caption']")))).build().perform();
WebElement clicking = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", clicking);
}
I use JS click, don't know why cannot click normally.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]")));
WebElement clicking = driver.findElement(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true); arguments[0].click()", clicking);
Apply scrollIntoView and then click on the USD using JavascriptExecutor.
JavascriptExecutor js = (JavascriptExecutor) driver;
WebDriverWait wait = new WebDriverWait(driver,30);
driver.get("https://alfabank.ru/currency/");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]")));
WebElement usdoption = driver.findElement(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]"));
js.executeScript("arguments[0].scrollIntoView(true);", usdoption);
js.executeScript("arguments[0].click();", usdoption);

Unable to click on submenu using Selenium Java

I'm trying to click on a submenu in a webpage. Submenu will be displayed when I just hover over the main menu. I have referred many solutions and it sill doesn't work.
And there are no iframes on the page.
Below is the code:
Actions ac = new Actions(d);
WebElement we = d.findElement(By.xpath("(//ul[#id='menu-top-menu']//li)[1]"));
ac.moveToElement(we).perform();
WebDriverWait wait = new WebDriverWait(d, 10);
WebElement we2 = wait.until(
ExpectedConditions.elementToBeClickable(By.xpath("(//ul[#id='menu-top-menu']//li//ul//li[2]//a)[1]")));
ac.moveToElement(we2).click().perform();
Can someone please help me out with it?.
If it's not necessary to simulate mousein and click, you can get the href value of a element and direct access with driver.
You just need to check if element is present.
String xpathAction = "//ul[#id='menu-top-menu'] /li[1] /ul /li /a[text() = 'Action']";
WebDriverWait wait = new WebDriverWait(driver, 10);
String hrefAction = wait.until(
ExpectedConditions.presenceOfElementLocated(By.xpath(xpathAction))
).getAttribute("href");
driver.get(hrefAction);

Selenium: Unknown error unable to click a custom made drop down at a point though it is there [duplicate]

I used explicit waits and I have the warning:
org.openqa.selenium.WebDriverException:
Element is not clickable at point (36, 72). Other element would receive
the click: ...
Command duration or timeout: 393 milliseconds
If I use Thread.sleep(2000) I don't receive any warnings.
#Test(dataProvider = "menuData")
public void Main(String btnMenu, String TitleResultPage, String Text) throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 10);
driver.findElement(By.id("navigationPageButton")).click();
try {
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(btnMenu)));
} catch (Exception e) {
System.out.println("Oh");
}
driver.findElement(By.cssSelector(btnMenu)).click();
Assert.assertEquals(driver.findElement(By.cssSelector(TitleResultPage)).getText(), Text);
}
WebDriverException: Element is not clickable at point (x, y)
This is a typical org.openqa.selenium.WebDriverException which extends java.lang.RuntimeException.
The fields of this exception are :
BASE_SUPPORT_URL : protected static final java.lang.String BASE_SUPPORT_URL
DRIVER_INFO : public static final java.lang.String DRIVER_INFO
SESSION_ID : public static final java.lang.String SESSION_ID
About your individual usecase, the error tells it all :
WebDriverException: Element is not clickable at point (x, y). Other element would receive the click
It is clear from your code block that you have defined the wait as WebDriverWait wait = new WebDriverWait(driver, 10); but you are calling the click() method on the element before the ExplicitWait comes into play as in until(ExpectedConditions.elementToBeClickable).
Solution
The error Element is not clickable at point (x, y) can arise from different factors. You can address them by either of the following procedures:
1. Element not getting clicked due to JavaScript or AJAX calls present
Try to use Actions Class:
WebElement element = driver.findElement(By.id("navigationPageButton"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
2. Element not getting clicked as it is not within Viewport
Try to use JavascriptExecutor to bring the element within the Viewport:
WebElement myelement = driver.findElement(By.id("navigationPageButton"));
JavascriptExecutor jse2 = (JavascriptExecutor)driver;
jse2.executeScript("arguments[0].scrollIntoView()", myelement);
3. The page is getting refreshed before the element gets clickable.
In this case induce ExplicitWait i.e WebDriverWait as mentioned in point 4.
4. Element is present in the DOM but not clickable.
In this case induce ExplicitWait with ExpectedConditions set to elementToBeClickable for the element to be clickable:
WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.elementToBeClickable(By.id("navigationPageButton")));
5. Element is present but having temporary Overlay.
In this case, induce ExplicitWait with ExpectedConditions set to invisibilityOfElementLocated for the Overlay to be invisible.
WebDriverWait wait3 = new WebDriverWait(driver, 10);
wait3.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("ele_to_inv")));
6. Element is present but having permanent Overlay.
Use JavascriptExecutor to send the click directly on the element.
WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
In case you need to use it with Javascript
We can use arguments[0].click() to simulate click operation.
var element = element(by.linkText('webdriverjs'));
browser.executeScript("arguments[0].click()",element);
I ran into this error while trying to click some element (or its overlay, I didn't care), and the other answers didn't work for me. I fixed it by using the elementFromPoint DOM API to find the element that Selenium wanted me to click on instead:
element_i_care_about = something()
loc = element_i_care_about.location
element_to_click = driver.execute_script(
"return document.elementFromPoint(arguments[0], arguments[1]);",
loc['x'],
loc['y'])
element_to_click.click()
I've also had situations where an element was moving, for example because an element above it on the page was doing an animated expand or collapse. In that case, this Expected Condition class helped. You give it the elements that are animated, not the ones you want to click. This version only works for jQuery animations.
class elements_not_to_be_animated(object):
def __init__(self, locator):
self.locator = locator
def __call__(self, driver):
try:
elements = EC._find_elements(driver, self.locator)
# :animated is an artificial jQuery selector for things that are
# currently animated by jQuery.
return driver.execute_script(
'return !jQuery(arguments[0]).filter(":animated").length;',
elements)
except StaleElementReferenceException:
return False
You can try
WebElement navigationPageButton = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("navigationPageButton")));
navigationPageButton.click();
Scrolling the page to the near by point mentioned in the exception did the trick for me. Below is code snippet:
$wd_host = 'http://localhost:4444/wd/hub';
$capabilities =
[
\WebDriverCapabilityType::BROWSER_NAME => 'chrome',
\WebDriverCapabilityType::PROXY => [
'proxyType' => 'manual',
'httpProxy' => PROXY_DOMAIN.':'.PROXY_PORT,
'sslProxy' => PROXY_DOMAIN.':'.PROXY_PORT,
'noProxy' => PROXY_EXCEPTION // to run locally
],
];
$webDriver = \RemoteWebDriver::create($wd_host, $capabilities, 250000, 250000);
...........
...........
// Wait for 3 seconds
$webDriver->wait(3);
// Scrolls the page vertically by 70 pixels
$webDriver->executeScript("window.scrollTo(0, 70);");
NOTE: I use Facebook php webdriver
If element is not clickable and overlay issue is ocuring we use arguments[0].click().
WebElement ele = driver.findElement(By.xpath("//div[#class='input-group-btn']/input"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
The best solution is to override the click functionality:
public void _click(WebElement element){
boolean flag = false;
while(true) {
try{
element.click();
flag=true;
}
catch (Exception e){
flag = false;
}
if(flag)
{
try{
element.click();
}
catch (Exception e){
System.out.printf("Element: " +element+ " has beed clicked, Selenium exception triggered: " + e.getMessage());
}
break;
}
}
}
In C#, I had problem with checking RadioButton,
and this worked for me:
driver.ExecuteJavaScript("arguments[0].checked=true", radio);
Can try with below code
WebDriverWait wait = new WebDriverWait(driver, 30);
Pass other element would receive the click:<a class="navbar-brand" href="#"></a>
boolean invisiable = wait.until(ExpectedConditions
.invisibilityOfElementLocated(By.xpath("//div[#class='navbar-brand']")));
Pass clickable button id as shown below
if (invisiable) {
WebElement ele = driver.findElement(By.xpath("//div[#id='button']");
ele.click();
}

Unable To Click Paypal Button Continue With Selenium

My code begins by signing me into PayPal, then signing into eBay and navigating to the pay fees page, then checking out with PayPal. The final "Continue" button I can't click/submit. I've tried by xpath, id and class. I even tried sending TAB 7x until the Continue button and then sending Enter but that didn't work.
I have found this discussion but I'm not sure how to make it work for me.
PayPal Sandbox checkout 'continue button' - Unable to locate element: - C# WebDriver
Here's a screenshot of the PayPal code and page I'm trying to do.
//Chrome WebDriver specific
System.setProperty("webdriver.chrome.driver", "C:\\automation\\drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize(); //maximise webpage
WebDriverWait wait = new WebDriverWait(driver, 20);
//navigate to Paypal
driver.get("https://www.paypal.com/uk/signin");
//wait 2.5s for the page to load
try {
Thread.sleep(2500);
}
catch (Exception e) {
e.printStackTrace();
}
WebElement paypalEmail = driver.findElement(By.id("email"));
paypalEmail.sendKeys("******");
//wait 2.5s for the page to load
try {
Thread.sleep(2500);
}
catch (Exception e) {
e.printStackTrace();
}
WebElement paypalSubmit = driver.findElement(By.id("btnNext"));
paypalSubmit.click();
String URL = ("https://www.paypal.com/uk/signin");
driver.get(URL);
WebElement form2 = driver.findElement(By.cssSelector(".main form"));
WebElement username = form2.findElement(By.id("password"));
username.sendKeys("******");
WebElement paypalSubmit2 = driver.findElement(By.id("btnLogin"));
paypalSubmit2.click();
//navigate to Ebay
driver.get("https://signin.ebay.co.uk/ws/eBayISAPI.dll?SignIn&ru=https%3A%2F%2Fwww.ebay.com%2F");
// Enter user name , password and click on Signin button
WebElement form = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#mainCnt #SignInForm")));
form.findElement(By.cssSelector("input[type=text][placeholder='Email or username']")).sendKeys("******");
form.findElement(By.cssSelector("input[type=password]")).sendKeys("******");
form.findElement(By.id("sgnBt")).click();
driver.get("http://cgi3.ebay.co.uk/ws/eBayISAPI.dll?OneTimePayPalPayment");
//WebElement Pay =
driver.findElement(By.xpath("//input[#value='Pay']")).click();
WebDriverWait wait2 = new WebDriverWait(driver, 15);
wait2.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id=\"confirmButtonTop\"]")));
driver.findElement(By.xpath("//*[contains(#id,'confirmButtonTop')]")).click();
}
}
Based on your given screenshot one of following should work to click on continue button :
Method 1 :
WebElement paypalSubmit = driver.findElement(By.xpath("//input[#data-test-id='continueButton']"));
paypalSubmit.click();
Method 2:
By paypalButton=By.xpath("//input[#data-test-id='continueButton']"));
WebElement element=driver.findElement(paypalButton);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);",element);
js.executeScript("arguments[0].click();", element);
Try 2nd method if you feel your button require bit scroll to bottom to get clickable.
one more xpaths you can use for button if above don't work :
//input[#value='Continue' and #id='confirmButtonTop']
In my experience, paypal likes to use iFrames. If that's true in your case, that means unless you tell webdriver to switch frame contexts, that paypal form will be unavailable to you regardless of your xpath/css selectors.
You can get a list of all available frames currently loaded with this code:
String[] handles = driver.getWindowHandles()
Your actual page will always be the 0th index in that returned array. If paypal is your only iFrame, then you can target the 1th index. Here's a possible solution to that:
String mainPageHandle = handles[0];
String paypalHandle = handles[1];
driver.switchTo().window(paypalHandle);
// Do paypal interactions
driver.switchTo().window(mainPageHandle);
// Back to the main page
There are definitely more robust ways to handle this, and if your page unfortunately has more than one iFrame, then you may need to do more to verify which handle is which, such as test the presence of an element you know is contained within. In general, the frames will load in the same order every time. As a golden path to this problem, this will get you in and out of that iFrame to perform work.
Sometimes the conventional click() doesn't work. In that case, try using the Javascript Executor Click as below.
Make sure you import this class
org.openqa.selenium.JavascriptExecutor
And use this instead of click();
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.xpath(“//input[#data-test-id='continueButton']”)));
Try this and let me know if this works for you.

Program terminates entering pass into textbox with webrunner

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C://chromedriver//chromedriver.exe");
WebDriver driver = new ChromeDriver();
//Maximize the Browser window
driver.manage().window().maximize();
driver.get("https://google.com");
WebElement signin = driver.findElement(By.id("gb_70"));
signin.click();
WebElement username = driver.findElement(By.id("Email"));
username.sendKeys("email#gmail.com");
WebElement next = driver.findElement(By.id("next"));
next.click();
WebElement password = driver.findElement(By.id("Passwd"));
password.sendKeys("password");
WebElement next1 = driver.findElement(By.id("signIn"));
next1.click();
My program terminates when it gets to the password entry screen.. it says
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"Passwd"}
(Session info: chrome=53.0.2785.143)
However, I have tried ID element and Xpath element and they are both correct
:
(
Possibly you need to wait after next.click(); command. After clicking next button, your input filed is not loaded yet. So, wait for the element to get loaded and put text in it.
Edit:
You can use explicit wait for this. It's better way then Thread.sleep(). Specify a maximum time to wait for element. If the specified time elapsed before your element is visible then it will throw a exception.
Code snippet:
WebDriverWait wait = new WebDriverWait(driver, 30); // waiting for maxiumum of 30 seconds
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("Password")));

Categories