I want to switch my webdriver to iframe before entering email id in the email id box. I am not able to locate the iframe that surrounds the email id box.
How do i locate iframe in order to make below code work?
Below is the java webdriver code,
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class HandlingIframes {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.firstcry.com/");
Thread.sleep(3000);
driver.switchTo().frame(driver.findElement(By.xpath(".//*[#id='amt']/div[2]"))); //This locator is not working.
driver.findElement(By.xpath(".//*[#id='Email']")).sendKeys("happita#gmail.com");
Thread.sleep(4000);
}
}
I went to the page source and found the id of the iframe and used it. Below code is working fine now.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class HandlingIframes {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.firstcry.com/");
Thread.sleep(3000);
driver.switchTo().frame("iframe_Login");
driver.findElement(By.xpath(".//*[#id='Email']")).sendKeys("happita#gmail.com");
Thread.sleep(4000);
}
}
Related
My Xpath is correct & no iFrame and I can locate element in Chrome console but my program still fails. I have used explicit wait also.
Website: https://qrgo.page.link/8YEcD I am trying to locate the male gender radio button.
I can locate the element in the same chromedriver console but still it shows no element found exception.
Code:-
package automation;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class residence {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", ".\\lib\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://serviceonline.bihar.gov.in/resources/homePage/10/loginEnglish.htm");
driver.findElement(By.xpath("//label[contains(text(),'General')]")).click();
driver.findElement(By.xpath(("//p[contains(text(),'Residential')]"))).click();
driver.findElement(By.xpath(("//div[#id='collapseOneOne']/div/p/a"))).click();
/* Write Gender accordingly.Default is Male(M).(F) and (T)*/
char gender='M';
WebDriverWait wait=new WebDriverWait(driver,Duration.ofSeconds(30));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='17290_1']")));
if(gender=='M')
{
driver.findElement(By.xpath("//input[#id='17290_1']")).click();
}
else if(gender=='F')
{
driver.findElement(By.xpath("//input[#id='17290_2']")).click();
}
else
{
driver.findElement(By.xpath("//input[#id='17290_3']")).click();
}
}
}
Here is a screenshot of the chrome window in which the testcase run:
Here also u can see that element is visible
Error message image
When click on the link, it is opening a new tab, you need to switch to new tab before accessing the element.
package automation;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class residence {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", ".\\lib\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://serviceonline.bihar.gov.in/resources/homePage/10/loginEnglish.htm");
// Store the current window handle
String parent_handle = driver.getWindowHandle();
driver.findElement(By.xpath("//label[contains(text(),'General')]")).click();
driver.findElement(By.xpath(("//p[contains(text(),'Residential')]"))).click();
driver.findElement(By.xpath(("//div[#id='collapseOneOne']/div/p/a"))).click();
// Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
if(!parent_handle.equals(winHandle))
{
driver.switchTo().window(winHandle);
}
}
/* Write Gender accordingly.Default is Male(M).(F) and (T)*/
char gender='M';
WebDriverWait wait=new WebDriverWait(driver,Duration.ofSeconds(30));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#for='17290_1']")));
if(gender=='M')
{
driver.findElement(By.xpath("//label[#for='17290_1']")).click();
}
else if(gender=='F')
{
driver.findElement(By.xpath("//label[#for='17290_2']")).click();
}
else
{
driver.findElement(By.xpath("//label[#for='17290_3']")).click();
}
}
}
I'm tring to send the login credentials within the username and password field https://www.vignanits.ac.in/server/moodle/login/index.php which needed to be automated using HtmlUnitDriver but facing NoSuchElementException.
Code trials:
package leela;
import org.openqa.selenium.By;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import com.gargoylesoftware.htmlunit.WebClient;
public class LeelaHtmlUnit {
public static void main(String[] args) throws InterruptedException {
HtmlUnitDriver driver = new HtmlUnitDriver(true);
driver.get("https://www.vignanits.ac.in/server/moodle/login/index.php");
System.out.println(driver.getCurrentUrl()+driver.getTitle());
driver.findElement(By.id("username")).sendKeys("xyz");
driver.findElement(By.id("password")).sendKeys("xyz");
driver.findElement(By.id("loginbtn")).click();
System.out.println(driver.getCurrentUrl()+driver.getTitle());
}
}
enter code here
Ideally, to use htmlunit-driver you need to download htmlunit-driver-2.42.0-jar-with-dependencies.jar the latest release as of today.
The below code block works perfect at my end:
import org.openqa.selenium.By;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class HtmlUnitDriverDemo {
public static void main(String[] args) {
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.get("https://www.vignanits.ac.in/server/moodle/login/index.php");
System.out.println(driver.getCurrentUrl()+driver.getTitle());
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("username")));
driver.findElement(By.id("password")).sendKeys("xyz");
driver.findElement(By.id("loginbtn")).click();
}
}
Console Output:
https://www.vignanits.ac.in/server/moodle/login/index.phpVIGNAN MOODLE: Log in to the site
Reference
You can find a detailed discussion on NoSuchElementException in:
NoSuchElementException, Selenium unable to locate element
I am trying to get the Error message printed in SalesForce website for entering incorrect username and password
package today;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Gmail {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe"); WebDriver mail=new ChromeDriver();
mail.get("https://login.salesforce.com/?locale=in");
mail.findElement(By.cssSelector("#username")).sendKeys("judinkp#gmail.com");
mail.findElement(By.xpath("//*[#id=\"password\"]")).sendKeys("23232");
mail.findElement(By.id("Login")).click();
System.out.println(mail.findElement(By.xpath("//*[#id='error']")).getText());
}
}
My Script runs till it clicks on Login but the Error message printed in website is not getting printed in my console , I am getting below error message
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='loginError']"}
(Session info: chrome=80.0.3987.149)
The Xpath is Browser given Xpath.
Website Link:https: //login.salesforce.com/?locale=in
Can you please try below solution:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class LoginScreen {
public static void main(String[] args) {
System.out.println("launching chrome browser");
System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to("https://login.salesforce.com/?locale=in");
driver.get("https://login.salesforce.com/?locale=in");
driver.findElement(By.cssSelector("#username")).sendKeys("judinkp#gmail.com");
driver.findElement(By.id("password")).sendKeys("23232");
driver.findElement(By.id("Login")).click();
WebDriverWait wait = new WebDriverWait(driver,20);
WebElement error= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("error")));
System.out.println(error.getText());
}
}
I had the same issue with CSS and Xpath locators.
Try with System.out.println(mail.findElement(By.id("error")).getText()); it should print out in the console.
package automation;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MainPage {
private final WebDriver driver;
public MainPage(WebDriver driver) {
this.driver = driver;
}
public MainPage loginAs(String username, String password) {
driver.get("URL");
driver.findElement(By.name("email")).sendKeys("username");
driver.findElement(By.name("password")).sendKeys("password");
driver.findElement(By.className("login")).click();
return new MainPage(driver);
}
public static void main(String[] args) {
System.setProperty("webdriver.genko.driver", "C:\\Users\\Guest01\\Desktop\\chromedriver");
MainPage login = new MainPage(new ChromeDriver());
login.loginAs("qa#gmail.com", "123456");
}
}
I tried to test login function for the web site and wrote above code for it. However,
I couldnt find the error on it. Can someone help me to figure it out?
I see a few things wrong with your code:
System.setProperty("webdriver.genko.driver", "C:\\Users\\Guest01\\Desktop\\chromedriver");
Should be (make sure the chromedriver is the correct file, the windows version usually has an .exe extension.)
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Guest01\\Desktop\\chromedriver");
You also should navigate the url of the page you want to test using
WebDriver driver = new ChromeDriver();
driver.get("http://[enter url here]")
Please write the "gecko driver" instead of "genko driver".
The website I am trying to automate is betting website and I have a scenario to automate a horse betting.
I am using selenium 3.0 with Java
From the site I am able to travel to horse race but unable to select Tomorrow and select the race. I tried using xpath, class and other methods but unable to click on these button.
website is
https://www.williamhill.com.au/
1 step. go to the above url
2. Select horse racing from top left corner or navigate to url (https://www.williamhill.com.au/racing?event=horseracing)
3.Click on Tomorrow I am unable to do this
4. Select on particular race from the table (unable to this too)
package automationFramework;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class horseRacing {
public static void main(String[] args) throws Exception {
String exePath = "D:\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", exePath);
WebDriver driver = new ChromeDriver();
//Launch the Online Store Website
driver.get("https://www.williamhill.com.au/");
Thread.sleep(5);
driver.manage().window().maximize();
String Title = driver.getTitle();
System.out.println(Title);
driver.findElement(By.className("MenuItem_text_N8V")).click();
Thread.sleep(25);
// driver.findElement(By.className("RaceGrid_raceTile_imG RaceGrid_raceDisabled_Q0m")).click();
driver.findElement(By.xpath("//*[#id='app']/div/div[4]/div/div/div/div[2]/div[1]/div/div[2]/div[2]/div[2]")).click();
// Print a Log In message to the screen
System.out.println("Successfully opened the website www.Store.Demoqa.com");
//Wait for 5 Sec
Thread.sleep(5);
// Close the driver
// driver.quit();
}
}
I tried to click "TOMORROW" and succeeded.
package test;
import org.openqa.selenium.By;
import org.junit.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NavigateToAUrl {
#Test
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.williamhill.com.au/racing?event=horseracing");
driver.findElement(By.xpath(".//*[#id='app']/div/div[4]/div/div/div/div[2]/div[1]/div/div[2]/div[2]/div[2]")).click();
}
}
Dot(.) is missing in your code driver.findElement(By.xpath("//*[#id='app']/div/div[4]/div/div/div/div[2]/div[1]/div/div[2]/div[2]/div[2]")).click();
(Before //*[#id~)