Cant select elements on Webpage - java

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

Related

Facebook post using java selenium

I am trying to post some text and pictures on facebook with a little Java program.
I am not able to post anything since I cannot click the appropriate button with selenium.
Here is the part of code where I need to click the upload Photo/video button on:
package good;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.interactions.Actions;
public class HareKrishna {
public static void main (String []args ) throws InterruptedException {
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver","/home/gt8201/AjaySingh/Automation Tools /chromedriver_linux64/chromedriver_linux64(1)/chromedriver");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.facebook.com/");
driver.findElement(By.name("email")).sendKeys("abc");
driver.navigate().forward();
driver.findElement(By.id("pass")).sendKeys("abx");
driver.findElement(By.name("login")).click();
driver.manage().window().maximize();
Thread.sleep(5000);
Alert alert = driver.switchTo().alert();
driver.switchTo().alert().accept();
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
driver.navigate().forward();
Thread.sleep(5000);
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
Actions Act = new Actions(driver);
driver.findElement(By.xpath("//*[#id=\"mount_0_0_08\"]/div[1]/div[1]/div/div[3]/div/div/div[1]/div[1]/div/div[2]/div/div/div/div[3]/div/div[2]/div/div/div/div[1]/div/div[1]/span"));
Act.click().sendKeys("Hare krishna");
}
}
The first step is click on this xpath //*[#class="sjgh65i0"]//descendant::div[#class="m9osqain a5q79mjw gy2v8mqq jm1wdb64 k4urcfbm qv66sw1b"] and it do to appear the pop-up "create publication".
Now you could add a comment with xpath //*[#method="POST"]//descendant::*[#role="presentation"]//descendant::*[#role="textbox"] or push button "add videos/photos" //*[#method="POST"]//descendant::div[#class="rq0escxv l9j0dhe7 du4w35lb j83agx80 cbu4d94t buofh1pr tgvbjcpo"]//descendant::*[#role="button"]
Later you could push button publish //*[#method="POST"]//descendant::div[#class="k4urcfbm discj3wi dati1w0a hv4rvrfc i1fnvgqd j83agx80 rq0escxv bp9cbjyn"]//descendant::*[#role="button"]

Java Selenium WebDriver: Click on Instagram Login Button

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

Not able to perform drag & drop using Actions

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!");

Selenium "Cannot locate element used to submit form"

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

Can we use Web driver to test https:// sites? I am trying to work on gmail.com and it doesn't work

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.

Categories

Resources