How do I fill the email section using chrome driver - java

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();
}

Related

selenium java NGWebdriver

I have tried everything to login into one site using Selenium webdriver java, but there is one "window", which I don't know how to call it , that I couldn't find one way to click in order to access it. Here, I open the firefox browser and lend on the parfumo.net webpage. The site loads "one window" with cookies settings...
public static void invokeBroser() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
NgWebDriver ngWebDriver = new NgWebDriver(js);
ngWebDriver.waitForAngularRequestsToFinish();
driver.get("https://www.parfumo.net");
driver.manage().window().maximize();
Thread.sleep(3000);
// Initialize and wait till element(link) became clickable - timeout in 60 seconds
WebDriverWait w = (WebDriverWait) new WebDriverWait(driver, Duration.ofSeconds(600));
w.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Accept*')]")));
}
There is a Iframe you have to first switch to it like below
driver.switchTo().frame("iframeId");
OR
driver.switchTo().frame("iframeName");
Once you switch to the frame than perform the click like below.
public static void invokeBroser() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
NgWebDriver ngWebDriver = new NgWebDriver(js);
ngWebDriver.waitForAngularRequestsToFinish();
driver.get("https://www.parfumo.net");
driver.manage().window().maximize();
Thread.sleep(3000);
// Initialize and wait till element(link) became clickable - timeout in 60 seconds
WebDriverWait w = (WebDriverWait) new WebDriverWait(driver, Duration.ofSeconds(600));
driver.switchTo().frame("sp_message_iframe_737779");
w.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Accept*')]")));
}
You can switch back to the main document like below:
driver.switchTo().defaultContent();

Selenium: Page gets refreshed after login

After the code clicks the action to login, the page gets refreshed and doesn't redirect to the next page. This only occurs for this website as I am redirected properly on other sites.
When I am giving the wrong credentials even the error message is not getting displayed.(Manually it works)
I am testing in Chrome with Selenium java.
Here is my code:
public class Test {
private static final String HTMLPageSourceCode = null;
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C:\\Selenium project\\chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://-----/tandem/login");
Thread.sleep(5000);
driver.manage().deleteAllCookies();
driver.findElement(By.id("login")).sendKeys("----");
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
driver.findElement(By.id("password")).sendKeys("----");
WebElement loginbutton = driver.findElement(By.xpath(".//*[#id='login-button']"));
Actions actions = new Actions(driver);
actions.click(loginbutton).perform();
}
}
To login in into the website https://outhouse.inhouseusa.com/tandem/login/ you need to induce WebDriverWait for the elementToBeClickable() and you can use the following Locator Strategies:
Code Block:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
//options.addArguments("disable-infobars");
WebDriver driver = new ChromeDriver(options);
driver.get("https://outhouse.inhouseusa.com/tandem/login/");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.form-control#login"))).sendKeys("sandy");
driver.findElement(By.cssSelector("input.form-control#password")).sendKeys("sandy");
driver.findElement(By.cssSelector("input.pull-right.button.button-primary#login-button")).click();

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element while trying to login through Selenium with Java

I have tried my best to write a login script in Selenium for the following site https://www.topmba.com/app. Here is my code:
public class TopMba {
String driverPath = "/usr/bin/chromedriver";
WebDriver driver;
String username = "test#gmail.com"; // Change to your username and passwrod
String password = "12345";
// This method is to navigate topmba URL
#BeforeClass
public void init() {
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
driver.navigate().to("https://www.topmba.com");
}
// To log in topmba
#Test
public void login() {
driver.findElement(By.className("tm-user")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.switchTo().frame(driver.findElement(By.xpath("//*[#id=\"tm-modal-frame-nvtfa7vvbm\"]")));
driver.findElement(By.id("edit-user")).sendKeys(username);
driver.findElement(By.id("edit-pass")).sendKeys(password);
driver.findElement(By.id("edit-submit")).click();
driver.switchTo().defaultContent();}
#AfterClass
public void quit() {
driver.close();
}
Here is the Exception :
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="tm-modal-frame-nvtfa7vvbm"]"}
Use the below code:
driver.get("https://www.topmba.com");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
String parentWindowHandle = driver.getWindowHandle();
driver.findElement(By.className("tm-user")).click();
WebElement iframe = driver.findElement(By.xpath("//iframe[contains(#src,'app/sso/user/login')]"));
driver.switchTo().frame(iframe);
driver.findElement(By.id("edit-name")).sendKeys(username);
driver.findElement(By.id("edit-pass")).sendKeys(password);
driver.findElement(By.id("edit-submit")).click();
driver.switchTo().window(parentWindowHandle);
There are a couple of things you need to take care as follows :
To click on the icon with tooltip as Login you have used :
driver.findElement(By.className("tm-user")).click();
If you look at the HTML this Locator Strategy identifies the element uniquely but for a more focused click you need to target the <span> tag which is within an <a> tag which is within the <li> tag with the class attribute you have used. Of-coarse you have induce WebDriverWait.
Once the Sign In Dialog Box opens up you will observe the login fields are within an <iframe>. So you have to induce WebDriverWait for both the cases, once for the frame to be available and for the desired element to be clickable and you can use the following solution :
Code Block :
System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.topmba.com");
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("li.tm-user>a.tmba-user>span.fa-img-icons.fa-img-user"))).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#src='/app/sso/user/login']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.input-block-level.form-text.required.form-control#edit-name"))).sendKeys("khawar");
driver.findElement(By.cssSelector("input.input-block-level.form-text.required.form-control#edit-pass")).sendKeys("khawar");
driver.findElement(By.cssSelector("button.btn.btn-warning.btn-block.button.js-form-submit.form-submit#edit-submit")).click();
Browser Snapshot :

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();

Webdriver :Element is not clickable at point (119, 9). Other element would receive the click

Scenario:
Login to www.flipkart.com and choose "Samsung" from "Electronics" after successful login.
Now, I need to scroll to the bottom of the page and from the left side, I need click on Availability to choose the "Exclude out of stock option" but
on clicking Availability, I am getting the message
FAILED: Test_Samsung
org.openqa.selenium.WebDriverException: Element is not clickable at point (119, 9). Other element would receive the click: <div class="_1H5F__" data-reactid="10"></div>
Command duration or timeout: 133 milliseconds
//class for successful flipkart login
public class Flipkart_Login
{
#FindBy(xpath="//a[text()='Log In']") WebElement Login_Click;
#FindBy(xpath="//input[#class='_2zrpKA' and #type='text']") WebElement Enter_Email;
#FindBy(xpath="//input[#class='_2zrpKA _3v41xv' and #type='password']") WebElement Enter_Pass;
#FindBy(xpath="//button[#type='submit' and #class='_3zLR9i _1LctnI _36SmAs']") WebElement Login_Button;
#FindBy(xpath="//span[text()='Please enter valid Email ID/Mobile number']") WebElement Blank_Email;
#FindBy(xpath="//span[text()='Please enter Password']") WebElement Blank_Pass;
public void Valid_Login()
{
Login_Click.click();
Enter_Email.sendKeys("abc#gmail.com");
Enter_Pass.sendKeys("abcde");
Login_Button.click();
}
}
//class for choosing Samsung from Electronics menu and clicking Availability
public class Flipkart_Electronics_Samsung_Mobile
{
#CacheLookup
#FindBy(xpath="//a[#title='Electronics']//span[text()='Electronics']") WebElement Electronics_Menu;
#CacheLookup
#FindBy(xpath="//a[#title='Samsung']//span[text()='Samsung']") WebElement Samsung_Mobile_Click;
#CacheLookup
#FindBy(xpath="//div[#class='_3QT2gR _1AgMas']//div[text()='Availability']") WebElement Availability;
#CacheLookup
#FindBy(xpath="//div[#class='_1p7h2j']") WebElement Exclude_Out_Of_Stock;
public void Choose_Samsung_Mobile()
{
WebDriverWait wait = new WebDriverWait(driver, 30);
Actions act = new Actions(driver);
act.moveToElement(Electronics_Menu).perform();
act.click(wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#title='Samsung']//span[text()='Samsung']")))).build().perform();
WebElement Availability = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='_3QT2gR _1AgMas']//div[text()='Availability']")));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].scrollIntoView(true);", Availability);
Availability.click();
}
}
//class which calls the methods from above two classes
public class Flipkart_Electronics_Samsung_Search
{
WebDriver driver;
#Test
public void Test_Samsung()
{
driver = BrowserFactory.getBrowser("Firefox");
driver.get(DataProviderFactory.getConfig().getURL());
Flipkart_Login login = PageFactory.initElements(driver, Flipkart_Login.class);
login.Valid_Login();
Flipkart_Electronics_Samsung_Mobile Samsung = PageFactory.initElements(driver, Flipkart_Electronics_Samsung_Mobile.class);
Samsung.Choose_Samsung_Mobile();
}
}
You can try to click using JavascriptExecutor So you should replace below line :-
js.executeScript("arguments[0].scrollIntoView(true);", Availability);
Availability.click();
To
js.executeScript("arguments[0].click()", Availability);
Element is not clickable at point (119, 9). Other element would receive the click: Command duration or timeout: 133 milliseconds
It clearly says, the element we want to click is hidden by some other element div in this case, which would receive the click.
I think it is problem with UI which shouldn't hide the element, but you can try few things :
1. Maximize the window of browser from webdriver to see if element is still hidden
driver.manage().window().maximize()
Use JavaScript to click element
WebElement element = driver.findElement(By.<locator>);
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click()", element)

Categories