Facebook post using java selenium - java

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"]

Related

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

How to open browser and android at the same time and run the tests simultaneously?

I have a test scenario that is as follows, I start the application on android using appium, then I need to open the browser to authenticate some things, right after that I need to go back to android. How to do this process ? I thought of using multithread .
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
public class ioSampleTest {
public AndroidDriver<MobileElement> driver;
public WebDriverWait wait;
//Elements
String secondNewJob = "//android.widget.FrameLayout[2]/android.widget.LinearLayout/" +
"android.widget.RelativeLayout/android.widget.ImageView";
#BeforeMethod
public void setup () throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", "Galaxy Nexus API 24");
caps.setCapability("udid", "emulator-5554"); //DeviceId from "adb devices" command
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "7.0");
caps.setCapability("skipUnlock","true");
caps.setCapability("appPackage", "com.isinolsun.app");
caps.setCapability("appActivity","com.isinolsun.app.activities.SplashActivity");
caps.setCapability("noReset","false");
driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"),caps);
wait = new WebDriverWait(driver, 10);
}
#Test
public void basicTest () throws InterruptedException {
//Click and pass Splash
wait.until(ExpectedConditions.visibilityOfElementLocated
(By.id("com.isinolsun.app:id/animation_view"))).click();
//Click I am searching a job
wait.until(ExpectedConditions.visibilityOfElementLocated
(By.id("com.isinolsun.app:id/bluecollar_type_button"))).click();
//Notification Allow
if (driver.findElements(By.id("com.android.packageinstaller:id/permission_allow_button")).size()>0) {
driver.findElements(By.id("com.android.packageinstaller:id/permission_allow_button")).get(0).click();
}
WebDriver browserFireFox = new FirefoxDriver();
try {
browserFireFox.get("https://google.com/ncr");
browserFireFox.findElement(By.name("q")).sendKeys("cheese" + Keys.ENTER);
WebElement firstResult = wait.until(presenceOfElementLocated(By.cssSelector("h3>div")));
System.out.println(firstResult.getAttribute("textContent"));
} finally {
browserFireFox.quit();
}
wait.until(ExpectedConditions.visibilityOfElementLocated
(By.xpath(secondNewJob)));
}
#AfterMethod
public void teardown(){
driver.quit();
}
}
This is a sample code in java, I took the code from these sites Tutorial Appium Java and Selenium , and set up an example to make it a little easier to understand what I want to do.
Note The code may not work, it is just an example.
Is your application launching WebView from your android application to as what you have mentioned, "authentication" before you can do anything to your app?
I believe Appium's set context is what you need, the api will let you switch back and forth from native to webview vice versa.
But if you are insisting to test thru appium and at the same time to desktop browser with selenium, then it's not possible.

Cant select elements on Webpage

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

How to close pop up in selenium webdriver?

I am trying to close pop up using selenium web driver with java. I have tried different ways, but unable to succeed. Please help me.
package Demo;
import java.util.Set;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class YahooTest {
public static void main(String[] args) throws InterruptedException {
FirefoxDriver obj = new FirefoxDriver();
String url = "https://www.planyourjourney.com/";
obj.get(url);
obj.manage().window().maximize();
obj.findElement(By.xpath("html/body/div[12]/div/div/div[9]")).click();
obj.findElementByClassName("dyna-link-new-registration").click();
obj.findElement(By.linkText("B2B Login")).click();
Thread.sleep(4000);
obj.findElement(By.id("userid")).clear();
obj.findElement(By.id("userid")).sendKeys("1592jet#gmail.com");
obj.findElement(By.id("ulPassword")).clear();
obj.findElement(By.id("ulPassword")).sendKeys("spyj01");
obj.findElement(By.name("Next")).click();
Thread.sleep(2000);
obj.findElement(By.id("affilitetrainadvpage"));
Alert alert = obj.switchTo().alert();
alert.dismiss();
}
}
I have seen the source code of the web page and could not find any alert in it.
The pop-up is actually written inside a div tag.
Hence, remove the codes related to alert and use below code which uses xpath.
obj.findElement(By.xpath("//*[#id='affilitetrainadvpage']/span/a/img")).click();

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