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();
Related
I am new to coding and the Selenium WebDriver and I cannot figure out how to automate a login process for Instagram. I figured out how to input the username and password, but I was unable to figure out how to click on the login button.
Here is my code:
package com.company;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","chromedriver");
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.instagram.com/accounts/login/?hl=en&source=auth_switcher");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.name("username")).sendKeys("username");
driver.findElement(By.name("password")).sendKeys("password");
driver.findElement(By.xpath("//button[contains(#class, 'loginBtn')]")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
}
The class for the login button is dynamic so you cannot click on it using the classname. However, you can click it using the text of the button in the xpath and I have verified it by running it myself.
You can do it like:
driver.findElement(By.xpath("//div[text()='Log In']")).click();
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")));
}
}
My java code load the relevant home page but doesn't log In to the site.
My code:
package queries;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Q {
{
System.setProperty("webdriver.gecko.driver", "G:\\\\Software\\\\geckodriver-v0.18.0-win64\\\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://10.20.10.16/MaharajaICLQA");
I am trying to logIn to page and try to get to values in pages.In that case this code works only towards loading the homepage.here below is the code to log and for the rest of the process. This only load the Homepage and not login to the site, why is that happens?
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String baseUrl = null;
driver.get(baseUrl + ("http://10.20.10.16/MaharajaICLQA/Login.aspx?lo=1"));
((WebElement) driver.findElement(By.id("txtUserName"))).clear();
((WebElement)driver.findElement(By.id("txtUserName"))).sendKeys("Priyasad");
((WebElement) driver.findElement(By.id("txtPassword"))).clear();
((WebElement) driver.findElement(By.id("txtPassword"))).sendKeys("lakpriya");
((WebElement) driver.findElement(By.cssSelector("btnLogin_CD"))).click();
driver.navigate().to("http://10.20.10.16/MaharajaICLQA/Returns_CC.aspx");
WebElement element = driver.findElement(By.xpath(".//* [#id='cphbody_gvRowData_DXFooterRow']/td[20]"));
List<String> pageOneValues = new ArrayList<>();
pageOneValues.add(element.getText());
driver.navigate().to("http://10.20.10.16/MaharajaICLQA/Reports/CustomVNetSales.aspx");
WebElement element2 = driver.findElement(By.xpath(".//*[#id='cphbody_gvSales_DXDataRow14']/td[33]"));
Assert.assertEquals(pageOneValues.get(0), element2.getText());
}
}
These two lines
String baseUrl = null;
driver.get(baseUrl + ("http://10.20.10.16/MaharajaICLQA/Login.aspx?lo=1"));
will cause selenium to make a request to
nullhttp://10.20.10.16/MaharajaICLQA/Login.aspx?lo=1
^^^^
That's probably not what you want.
Currently, I am using Thread.sleep to make the scripts to wait for certain element to be loaded. Execution takes long time.. Instead this, i need something waitforElement once it is displayed i need to continue my execution rather this Thread.sleep..
Can someone tell me that the below logic looks ok??
package com.test.utility;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class WaitForElement extends globalVariables {
public static void waitfor(String locator) {
try {
WebDriverWait Test_Wait = new WebDriverWait(driver, 60);
WebElement locatorvalue = common.getObject(locator);
Boolean click = Test_Wait.until(ExpectedConditions
.elementToBeSelected(locatorvalue));
} catch (Throwable e) {
System.err.println("The element is not found");
}
}
}
I have used this similar code in my project and it works for me.
import java.util.concurrent.TimeUnit;
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.remote.DesiredCapabilities;
public class MyFirstSelTest {
public static void main(String args[]){
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.gmail.com/");
WebElement un = driver.findElement(By.id("Email"));
WebElement pwd = driver.findElement(By.id("Passwd"));
WebElement submitBtn = driver.findElement(By.id("wp-signIn"));
un.sendKeys("ValidUsername");
pwd.sendKeys("ValidPassword");
submitBtn.click();
driver.quit();
}
}
Gmail home page is opened, but no data is entered into username and password field. Can some one please help me as to what we need to do for this?
Thanks,
Mike
Try with below locators.
driver.findElement(By.cssSelector("div.email-div>input[id='Email']")).sendKeys(Email);
driver.findElement(By.cssSelector("div.passwd-div>input[id='Passwd']")).sendKeys(Email);
driver.findElement(By.id("signIn")).click();
For reasons I do not get the problem is the submit button :
Try to replace with : WebElement submitBtn = driver.findElement(By.id("signIn")); You didn't identify the submit button correctly.