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
Related
While clickig the element inside the iframe getting selenium timeout of no such frame element exception . It's the RedBus web application I am using.
I tried with the driver switch to ().the frame("gsi_934517_585705"); with id, Name, index too but no success.
WebDriver driver= new ChromeDriver();
driver.get("https://www.redbus.in/");
driver.manage().window().maximize();
driver.findElement(By.xpath("//div[#id='signin-block']")).click();
driver.findElement(By.xpath("//li[#id='signInLink' and text()='Sign In/Sign Up']")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2000));
//Thread.sleep(50000);
driver.switchTo().frame("gsi_934517_585705");
driver.findElement(By.xpath("//span[text()='Sign in with Google' and #class='nsm7Bb-HzV7m-LgbsSe-BPrWId']")).click();
driver.close();
The below line of code won't switch to the required iframe because the ID is dynamic and each time you execute, the iframe id is different.
driver.switchTo().frame("gsi_934517_585705");
Try the below code:
driver.get("https://www.redbus.in/");
driver.findElement(By.xpath("//div[#id='signin-block']")).click();
driver.findElement(By.xpath("//li[#id='signInLink' and text()='Sign In/Sign
Up']")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
int size = driver.findElements(By.tagName("iframe")).size();
//System.out.println("no of iframes" + size);
//There are 2 iframes,we are interested in the second one,hence the below line
driver.switchTo().frame(1);
driver.findElement(By.xpath("//span[text()='Sign in with Google' and #class='nsm7Bb-HzV7m-LgbsSe-BPrWId']")).click();
driver.close();
Basically, get the number of iframes and switch to the iframe you want based on its index. Good luck. Upvote if this answers and solves your query,
To click on the element Sign in with Google as the the desired element is within nested iframe you have to:
Induce WebDriverWait for the parent frameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the nested frameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the desired elementToBeClickable().
You can use either of the following locator strategies:
driver.get("https://www.redbus.in/");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#signin-block"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("ul.config-list > li#signInLink"))).click();
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe.modalIframe")));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title='Sign in with Google Button']")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Sign in with Google']"))).click();
Browser Snapshot:
Reference
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?
Is there any solution to bypass the cookie iframe by using selenium webdriver?
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.
I have the following code written in Java with Selenium Webdriver, but it's not clicking the div with the star rating
driver.get("https://goo.gl/maps/gLCX3PitJT1cXr9v9");
driver.findElement(By.xpath("//button[#data-value=\"Escribir una opinión\"]")).click();
driver.switchTo().frame(2);
driver.findElement(By.xpath("//textarea")).sendKeys("This is just a test");
driver.findElement(By.xpath("//span[#aria-label=\"Cuatro estrellas\"]")).click();
Between each line, I also added a Thread.sleep(5000) just to ensure the page fully loads.
The only clear identifier that I see is the aria-label.
Manual steps:
Open URL: https://goo.gl/maps/gLCX3PitJT1cXr9v9
Click "Escribir una opinión" or "Write a review" button
Type in the review field: "This is just a test"
Select the star rating: 4 stars
Click "Publicar" or "Post" (or Publish)
The Xpath for the "four-star" element is wrong. It should be //div[#aria-label='Cuatro estrellas']. You had span instead.
Can you try below
driver.findElement(By.xpath("//div[#class="s2xyy"][4]")).click();
OR
driver.findElement(By.xpath("//div[#role="radio"][3]")).click();
it is working for me
To click() on star rating 4 stars as the 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:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe.goog-reviews-write-widget")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div[aria-label='Four stars']"))).click();
Using xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#class='goog-reviews-write-widget']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#aria-label='Four stars']"))).click();
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
I am trying to inspect an element in Iframe
I am able to succesfully get into iframe but when trying to inspect element -Exception comes element not intertracble
Below is the html part and the search button I am trying to click
I tried xpath and inspection like following
//driver.findElement(By.className("gsc-search-button-v2")).click();
//driver.findElement(By.cssSelector("button.gsc-search-button")).click();
//driver.findElement(By.xpath("//button[text()='gsc-search-button']")).click();
//driver.findElement(By.cssSelector("button[class='gsc-search-button gsc-search-button-v2']")).click();
//driver.findElement(By.cssSelector(".gsc-search-button.gsc-search-button-v2")).click();
//driver.findElement(By.xpath("//button[contains(#class, 'gsc-search-button gsc-search-button-v2")).click();
//wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".gsc-search-button.gsc-search-button-v2"))).sendKeys(Keys.ENTER);
Also by giving waits also like
WebDriverWait wait = new WebDriverWait(driver, 30);
//wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".gsc-search-button.gsc-search-button-v2"))).sendKeys(Keys.ENTER);
html code
To click() on the element to search, as the 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:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe_cssSelector")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("td.gsc-search-button > button.gsc-search-button.gsc-search-button-v2"))).click();
Using xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("iframe_xpath")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[#class='gsc-search-button']/button[#class='gsc-search-button gsc-search-button-v2']"))).click();
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
How to click within the username field within ProtonMail signup page 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