clicking the download button is not working properly in selenium java - java

I did not face any error as no element found however my test case is passed in console but when i checked in download folder it shows some temp file instead of actual image file. It will be very useful if someone solve this issue.
driver.get("https://demoqa.com/elements");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Here normal 'findElement' is not working, hence used the javascript executor
WebElement leftmenu = driver.findElement(By.xpath("(//li[#id='item-7']//span)[1]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", leftmenu); //clicking the left menu
Thread.sleep(5000);
driver.findElement(By.xpath("//a[#download='sampleFile.jpeg']")).click(); // download button

You are finishing the test run and closing the browser immediately after clicking the download button.
Try adding a simple sleep after clicking it.
Also, you should not use hardcoded pauses like this
Thread.sleep(5000);
Explicit wait should be used instead.
Also please try to wait until the element is visible, as I wrote here. I think this will let you clicking it with regular driver .click() method.
Try this:
driver.get("https://demoqa.com/elements");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, globalDelay);
// Here normal 'findElement' is not working, hence used the javascript executor
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//li[#id='item-7']//span)[1]"))).click();
//WebElement leftmenu = driver.findElement(By.xpath("(//li[#id='item-7']//span)[1]"));
//JavascriptExecutor executor = (JavascriptExecutor)driver;
//executor.executeScript("arguments[0].click();", leftmenu); //clicking the left menu
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#download='sampleFile.jpeg']"))).click();
Thread.sleep(5000);

To click() on the element Download you can use either of the following Locator Strategies:
linkText:
driver.findElement(By.linkText("Download")).click();
cssSelector:
driver.findElement(By.cssSelector("a#downloadButton[download^='sampleFile']")).click();
xpath:
driver.findElement(By.xpath("//a[#id='downloadButton' and starts-with(#download, 'sampleFile')]")).click();
Ideally, to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
linkText:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Download"))).click();
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a#downloadButton[download^='sampleFile']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#id='downloadButton' and starts-with(#download, 'sampleFile')]"))).click();
PS: Once you click on Download don't close the web browser immediately and wait for sometime for the download process to be completed.

Related

How to close the promotion popup in makemytrip.com using selenium webdriver

I am not able to close the below window on "https://www.makemytrip.com/" website:
I tried with alert, notification and child browser popup, but nothing had worked out.
Please anyone help on this?
The object(close button) you should have hit to close the pop up is inside another frame.
So switch to that iframe first and then try to hit the close button. See below the code to switch frame.
driver.switchTo().frame("id of the element");
To click on the element X as the desired element is within a <iframe> so you have to:
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the desired elementToBeClickable.
You can use either of the following Locator Strategies:
Using cssSelector:
driver.get("https://www.makemytrip.com/");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title^='notification-frame']")));
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("i.we_close"))).click();
Using xpath:
driver.get("https://www.makemytrip.com/");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(#title, 'notification-frame')]")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//i[#class='wewidgeticon we_close']"))).click();
Browser Snapshot:
References
You can find a couple of relevant discussions in:
Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
Selenium: Can't click on a button within an iframe

Can not click on a button; - "element is not interactable"

I have a script where I'm trying to push a button on an ecommerce site but when I run a script I get "Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable " .
When I put xpath into the chropath, element is located so I guess path driver.findElement(By.xpath("//form/fieldset[2]/button")).click(); is right. Please see attached screenshots and be gentle I'm new to programming and this site :/
I see, there's an Accept cookies button, if you want to click on that, you can use the below code :-
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#onetrust-accept-btn-handler"))).click();
and then click on Sleeve length like this :
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//form/fieldset[2]/button"))).click();
Note that you should open browser in full screen before interacting any web element.
driver.manage().window().maximize();
driver.get("Your URL");
You could use a JavascriptExecutor to trigger the click:
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", yourWebElement);
But I would recommand also to use explicit/implicit waits instread of Thread.sleep (I've linked an article, you can find more information on google).

Unable to click or sendKeys in ajax overlay popup coming and its showing Element not Intractable exception using selenium and java

I have been practicing to automate few scenarios for web application way2Automation.com and struggling to enter the text in Registration form coming as the popup.
I have done some research already and tried many ways mentioned below:
a) Using WebDriverWait and explicit wait
b) Using Implicit wait and Thread.sleep
c) Using JavaScriptExecutor
But none of them worked for me and I am still stuck to register the user. Would really appreciate the help. Below are the artificats
URL: http://way2automation.com/way2auto_jquery/index.php
Code trials:
1)
// WebElement ele = driver.findElement(By.xpath("//*[#id='load_form']/div/div[2]/input"));
// JavascriptExecutor executor = (JavascriptExecutor)driver;
// executor.executeScript("arguments[0].click();", ele);
// WebElement button = driver.findElement(By.xpath("//*[#id=\"load_form\"]/div/div[2]/input"));
// new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(button));
You need to handle windows
Try the below code and let me know updates
driver.get("http://way2automation.com/way2auto_jquery/index.php");
String parent=driver.getWindowHandle();
String child=driver.getWindowHandle();
driver).switchTo().window(child);
driver.findElement(By.name("name")).sendKeys("Abhishek Saxena");
To click on SUBMIT you can use either of the following Locator Strategies:
cssSelector:
driver.findElement(By.cssSelector("input.button[value='Submit']")).click();
xpath:
driver.findElement(By.xpath("//input[#class='button' and #value='Submit']")).click();
However, as the element is a dynamic element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.button[value='Submit']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='button' and #value='Submit']"))).click();
Reference
You can find a detailed discussion on NoSuchElementException in:
NoSuchElementException, Selenium unable to locate element

Clicking buttons with Selenium WebDriver Java

There is a button on a website I am testing and Selenium can't seem to click it. I have tried many different ways but still nothing. Attached is the HTML for the button as well as the XPath.
Has anyone else experienced this or knows how to get around this?
XPath:
/html/body/div[1]/div/div/form/div[21]/div[3]/button
[HTML]
Could be a synchronization issue. kindly try below solution if your element is not within iframe or else you need to switch control to iframe first before dealing with the web element.
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"))).click();
Or You can also try javascript solution:
WebElement element= driver.findElement(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Try below code -
Actions action = new Actions(driver);
WebElement My_btn = webdriver.findElement(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"));
action.moveToElement(My_btn).click(My_btn).build().perform();
Let me know the outcome.

org.openqa.selenium.ElementClickInterceptedException: element click intercepted error using Selenium and Java in headless mode

I have a project that I am working on with java and selenium.
the test work OK in UI mode.
However in headless mode I get this error
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label _ngcontent-yrc-c26="" formcontrolname="reportingDealPermission" nz-checkbox="" class="ant-checkbox-wrapper ng-untouched ng-pristine ng-valid" ng-reflect-name="reportingDealPermission">...</label> is not clickable at point (161, 562). Other element would receive the click: <div _ngcontent-yrc-c26="" class="footer">...</div>
how can I resolve this issue (working in UI mode). this is my code
WebDriver driver = getWebDriver();
WebElement element;
Thread.sleep(60000);
element = driver.findElement(By.xpath("//label[#formcontrolname='reportingDealPermission']"));
element.click();
why in selenium there is no operation to move to the element and break all layers.
this is the UI.
this is working in UI mode not working in headless mode, made sleep for 6 minutes and not resolved so this is not time issue
This error message...
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label _ngcontent-yrc-c26="" formcontrolname="reportingDealPermission" nz-checkbox="" class="ant-checkbox-wrapper ng-untouched ng-pristine ng-valid" ng-reflect-name="reportingDealPermission">...</label> is not clickable at point (161, 562). Other element would receive the click: <div _ngcontent-yrc-c26="" class="footer">...</div>
...implies that the click on the desired element was intercepted by some other element.
Clicking an element
Ideally, while invoking click() on any element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[formcontrolname=reportingDealPermission][ng-reflect-name=reportingDealPermission]"))).click();
xpath:
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission' and #ng-reflect-name='reportingDealPermission']"))).click();
Update
After changing to headless if it still doesn't works and still get exception there still a couple of other measures to consider as follows:
Chrome browser in Headless mode doesn't opens in maximized mode. So you have to use either of the following commands/arguments to maximize the headless browser Viewport:
Adding the argument start-maximized
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("start-maximized");
WebDriver driver = new ChromeDriver(options);
Adding the argument --window-size
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--window-size=1400,600");
WebDriver driver = new ChromeDriver(options);
Using setSize()
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver driver = new ChromeDriver(options);
driver.manage().window().setSize(new Dimension(1440, 900));
You can find a detailed discussion in Not able to maximize Chrome Window in headless mode
Additionally, you can also wait for the intercept element to be invisible using the ExpectedConditions invisibilityOfElementLocated before attempting the click() as follows:
cssSelector:
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("div.footer")));
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[formcontrolname=reportingDealPermission][ng-reflect-name=reportingDealPermission]"))).click();
xpath:
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[#class='footer']")));
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission' and #ng-reflect-name='reportingDealPermission']"))).click();
References
You can find a couple of related relevant discussions in:
Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click
Element MyElement is not clickable at point (x, y)… Other element would receive the click
Try adding an explicit wait
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission']"))).click();
and if this doesn't work then try using the JS Executor
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission']")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
None of the above answers worked for me. Try using action class as follows:
WebElement element = driver.findElement(By.xpath("//div[#class='footer']"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
For this issue:
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label _ngcontent-yrc-c26="" formcontrolname="reportingDealPermission" nz-checkbox="" class="ant-checkbox-wrapper ng-untouched ng-pristine ng-valid" ng-reflect-name="reportingDealPermission">...</label> is not clickable at point (161, 562).
Another element receives the click:
Answer is to explicitly wait with javascript executor. This combination is working for me:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission']")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
WebDriverWait wait = new WebDriverWait(driver, 10); ---> has deprecated and it gives an error. Please use below instead:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission']"))).click();
Ensure to import relevant items for the WebDriverWait and ExpectedConditions.
In my case "JavaScript" works:
WebElement ele = driver.findElement(By.xpath("(//input[#name='btnK'])[2]"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click()", ele);
It was taken from:
http://makeseleniumeasy.com/2020/05/25/elementclickinterceptedexception-element-click-intercepted-not-clickable-at-point-other-element-would-receive-the-click/
Answer is worked for me.
Please check the below sreenshot for the my problem reference.
Below alert is comes in between the the my button and frame.
enter image description here
In my case, nothing worked. Only the THREAD.SLEEP() method worked!

Categories