I am facing a very strange problem. I am trying to open facebook>click on forgotten account link> then opening it in a new tab> click on two textboxes.
My code is :
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class OpenLinkInNewTabTest {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "<path>\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.facebook.com");
String ParentWindowHandle = driver.getWindowHandle();
WebElement w = driver.findElement(By.linkText("Forgotten account?"));
new Actions(driver)
.keyDown(Keys.CONTROL)
.keyDown(Keys.SHIFT)
.click(w)
.keyUp(Keys.SHIFT)
.keyUp(Keys.CONTROL)
.perform();
new Actions(driver)
.sendKeys(Keys.CONTROL + "w")
.perform();
// ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
//
// driver.switchTo().window(tabs.get(1));
// WebElement fn = (new WebDriverWait(driver, 20))
// .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#identify_email")));
// System.out.println(driver.getTitle());
// fn.sendKeys("abcdejf:");
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
if(driver.getTitle().contains("Forgotten Password ")){
Thread.sleep(5000);
driver.findElement(By.cssSelector("#identify_email")).sendKeys("adf");
driver.findElement(By.name("email")).sendKeys("ASF");
driver.close();
driver.switchTo().window(ParentWindowHandle);
break;
}
}
driver.findElement(By.name("email")).sendKeys("ASF");
}
}
However, I am unable to send value to
driver.findElement(By.cssSelector("#identify_email")).sendKeys("adf");
The element looks like:
<input id="identify_email" class="inputtext" name="email" autofocus="1" type="text">
If I write a similar code like:
System.setProperty("webdriver.chrome.driver","path\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.facebook.com");
driver.findElement(By.linkText("Forgotten account?")).click();
driver.findElement(By.name("email")).sendKeys("ASF");
driver.findElement(By.cssSelector("#identify_email")).sendKeys("adf");
It is able to click both the elements properly.
I am not seeing any element not found exception too while running it. Please help me to debug this issue.
Thanks.
UPDATE:
Running this code sometimes show
Exception in thread "main" org.openqa.selenium.InvalidElementStateException: invalid element state
(Session info: chrome=56.0.2924.87)
Wait for this element:
WebDriverWait wait = new WebDriverWait(driver, 10);
String email = "you#domain.com";
WebElement emailInputField = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#identify_email")));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].setAttribute('value', ' " + email + "')", emailInputField);
break;
Clicking activates this element.
Related
I am trying to Copy and paste OTP using java selenium but it's not working I am using xpath to copy OTP from yopmail using java selenium I am unable to verify if the OTP has been copied successfully or not
package Amb;
import graphql.schema.idl.SchemaGenerator;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
public class Loginusingtempmail {
public static void main(String[] args) throws AWTException {
System.setProperty("webdriver.chrome.driver", "/home/gt8201/Automation Tools /Chromedriver/chromedriver_linux64 (3)/chromedriver_linux64 (4)/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://yopmail.com/en/wm");
driver.manage().window().maximize();
driver.navigate().forward();
driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
driver.findElement(By.xpath(" //*[#id=\"login\"]")).sendKeys("bony#yopmail.com");
String windowHandle = driver.getWindowHandle();
//ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
//driver.switchTo().window(tabs.get(1));
driver.findElement(By.xpath("/html/body/div/div[2]/main/div[3]/div/div[1]/div[2]/div/div/form/div/div/div[4]/button/i")).click();
driver.navigate().forward();
//String strCode = driver.findElement(By.tagName("tr")).getText();
//driver.findElement(By.xpath(" /html/body/div[2]/div[4]/button/div[1]/span[2]")).click();
driver.navigate().forward();
String OTP = driver.findElement(By.xpath(" //*[#id=\"ifinbox\"]")).getText();
driver.findElement(By.xpath("//*[#id=\"ifinbox\"]")).sendKeys(OTP);
Actions actions = new Actions(driver);
WebElement Text = driver.findElement(By.xpath("//*[#id=\"ifinbox\"]"));
actions.moveToElement(Text).doubleClick().build().perform();
driver.findElement(By.xpath("//*[#id=\"ifinbox\"]")).sendKeys(Keys.chord(Keys.CONTROL,"c"));
driver.navigate().forward();
driver.findElement(By.xpath("//*[#id=\"ifinbox\"]")).sendKeys(Keys.chord(Keys.CONTROL,"v"));
System.out.println("The header text is - " +Text.getText());
}
}
[1]: https://i.stack.imgur.com/z3q2C.png
This is a sample code for the explicit wait:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath-here")));
I want to pass WebElement as a parameter in method and wait until that WebElement is located:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(MyWebElement));
I am not sure whether such option is already there and can be done in a different way as in my case I am getting an exception as I am passing WebElement in place of By.xpath("") which is not the correct way.
You need to use the visibilityOf expected condition.
The code will be like:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(MyWebElement));
Hope it helps you!
Yes, you cannot pass the web element in place of XPath because the method 'visibilityOfElementLocated()' will accept only By locator.
Suppose, your method is like below :
public static void waitUntilLocated(WebDriver driver, int waitingTime, By locator) {
new WebDriverWait(driver, waitingTime).until(ExpectedConditions.visibilityOfElementLocated(locator));
}
Then You can store the By locator like below :
By someXPath = By.xpath("some xpath here");
Then you can pass 'By locator' to the method as a parameter
waitUntilLocated(driver, 30, someXPath);
Below is the whole code :
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Test1 {
public static void waitUntilLocated(WebDriver driver, int waitingTime, By locator) {
new WebDriverWait(driver, waitingTime).until(ExpectedConditions.visibilityOfElementLocated(locator));
}
public static void main(String ...ali) throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("chrome");
WebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:1234/wd/hub"), capabilities);
driver.get("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("Alicse3"+Keys.ENTER);
// Checking element is located or not?
By someXPath = By.xpath("some xpath here");
waitUntilLocated(driver, 30, someXPath);
// Checking some other element
By someId = By.id("some id");
waitUntilLocated(driver, 30, someId);
}
}
Or you can do like below if you want to pass Web Element to the method and don't want to use 'visibilityOfElementLocated()' :
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Test1 {
public static void waitUntilLocated(WebDriver driver, int waitingTime, WebElement element) {
new WebDriverWait(driver, waitingTime).until(ExpectedConditions.visibilityOf(element));
}
public static void main(String ...ali) throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("chrome");
WebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:1234/wd/hub"), capabilities);
driver.get("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("Alicse3"+Keys.ENTER);
// Checking element is located or not?
WebElement element = driver.findElement(By.xpath("Some XPath"));
waitUntilLocated(driver, 30, element);
// Checking some other element
element = driver.findElement(By.id("Some ID"));
waitUntilLocated(driver, 30, element);
}
}
I hope it helps...
I'm having major issue trying to select menu two elements on a dropdown.I've tried xpaths, link texts and css selector but it wont select either the password button or logout button.
Xpaths used for Password button: "//*[#id='app']/header/div[3]/nav/ul/li/a"
CSS used for Logout button: ["data-logged-in-log-out-button"]
XPath used for Logout button: "//*[#id='app']/header/div[3]/nav/ul/a"
The error im getting for the select password is:
org.openqa.selenium.WebDriverException: unknown error: Element ... is not clickable at point (989, 233).
Other element would receive the click: ...
Please try with the below XPath along with the explicit wait condition.
XPath:
//*[contains(text(),'Log out')]
Can you please try -
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 A {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("url here");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Log Out")));
driver.findElement(By.linkText("Log Out")).click();;
}
}
If it still doesn't work, please try -
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class A {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("url here");
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", driver.findElement(By.linkText("Log Out")));
}
}
package Chrome_Packg;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class testFirefox_DragDrop {
public static void main(String[] args) throws InterruptedException {
WebDriver driver=new FirefoxDriver();
driver.get("http://jqueryui.com/droppable/");
WebElement drag=driver.findElement(By.xpath("/html/body/div[1]"));//drag element
WebElement drop=driver.findElement(By.xpath("/html/body/div[2]"));//drop element
Actions action=new Actions(driver);
Thread.sleep(3000);
action.dragAndDrop(drag, drop).perform();
}
}
After executing the code, using Run as java application, in output I am getting nothing.
Here is the code snippet for the same website that you are trying on and you can also find the example here selenium Drag and Drop example
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.navigate().to("http://jqueryui.com/droppable/");
//Wait for the frame to be available and switch to it
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(".demo-frame")));
WebElement Sourcelocator = driver.findElement(By.cssSelector(".ui-draggable"));
WebElement Destinationlocator = driver.findElement(By.cssSelector(".ui-droppable"));
dragAndDrop(Sourcelocator,Destinationlocator);
String actualText=driver.findElement(By.cssSelector("#droppable>p")).getText();
Assert.assertEquals(actualText, "Dropped!");
I am using the Selenium Library to try and log in to www.marketwatch.com for me. It can find all the elements, but upon calling the .submit() method, I get a "Cannot locate element used to submit form" error. When using button.click(), nothing happens at all. Thanks
package com.logansnow.marketwatch;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.Keys;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Main {
public static void main(String[] args){
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);
java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);
WebDriver driver = new HtmlUnitDriver();
driver.get("https://id.marketwatch.com/access/50eb2d087826a77e5d000001/latest/login_standalone.html?url=http%3A%2F%2Fwww.marketwatch.com%2Fuser%2Flogin%2Fstatus");
WebElement email = driver.findElement(By.name("username"));
WebElement loginPass = driver.findElement(By.name("password"));
WebElement button = driver.findElement(By.id("submitButton"));
email.sendKeys("***********#gmail.com");
loginPass.sendKeys("******************");
//loginPass.submit();
driver.findElement(By.className("login_submit")).click();
email.submit();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(driver.getTitle());
}
}
Problem seems to be stemming from email.submit(); statement. You need one of the two statements really to submit the form.
package com.logansnow.marketwatch;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.Keys;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Main {
public static void main(String[] args){
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);
java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);
WebDriver driver = new HtmlUnitDriver();
driver.get("https://id.marketwatch.com/access/50eb2d087826a77e5d000001/latest/login_standalone.html?url=http%3A%2F%2Fwww.marketwatch.com%2Fuser%2Flogin%2Fstatus");
WebElement email = driver.findElement(By.name("username"));
WebElement loginPass = driver.findElement(By.name("password"));
WebElement button = driver.findElement(By.id("submitButton"));
email.sendKeys("***********#gmail.com");
loginPass.sendKeys("******************");
//loginPass.submit();
// Click on the submit button to submit the form.
driver.findElement(By.className("login_submit")).click();
// Can also use this since you already have WebElement referencing the submit button.
// button.click();
// Or invoke submit on the button element
// button.submit();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(driver.getTitle());
}
}
Do submit using below . Just replace your submit code with below :
driver.findElement(By.id("submitButton")).click();
If your webpage contains javascript, HTMLUnitDriver doesn't enable javascript by default.
Make sure you are enabling javascript using the constructor parameter.
WebDriver driver = new HtmlUnitDriver();