I logged into Facebook. Now from home page or News Feed page I want to type some message in "Share an Update" text area and click on Post.
Tried below locator. Didn't work.
driver.findElement(By.name("xhpc_message_text")).sendkeys("Hello World");
WebDriverWait wait = new WebDriverWait(driver, 5);
Try the below code .. it must work for you..
import java.util.List;
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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Facebook_login {
public static void main(String[] args) throws InterruptedException {
String user_name = "facebook_user_name";
String pwd = "facebook_password";
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.name("email")).clear();
driver.findElement(By.name("email")).sendKeys(user_name);
driver.findElement(By.name("pass")).clear();
driver.findElement(By.name("pass")).sendKeys(pwd);
driver.findElement(By.xpath("//input[contains(#value,'Log In')]")).click();
Thread.sleep(10000);
System.out.println("logged in successfully");
WebElement notification = driver.findElement(By.xpath("//a[contains(#action,'cancel')]"));
if(notification.isDisplayed()) {
System.out.println("Notification is present");
notification.click();
}
WebElement status =driver.findElement(By.xpath("//textarea[#name='xhpc_message']"));
status.sendKeys("Hello");
Thread.sleep(3000);
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Post']"))).click();
}
}
I have found a solution for your question. Please try the below code:
Code:
url = "http://facebook.com";
String email = "email";
String password = "password";
System.setProperty("webdriver.chrome.driver", "src/chromedriver 3");
WebDriver driver = new ChromeDriver();
driver.get(url);
driver.manage().window().maximize();
driver.findElement(By.id("email")).sendKeys("your email id");
driver.findElement(By.id("pass")).sendKeys("Your password" + Keys.ENTER);
WebDriverWait wait = new WebDriverWait(driver, 500);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//textarea[contains(#title,'Share an update')]")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//textarea[contains(#title,'Share an update')]")));
driver.findElement(By.xpath("//textarea[contains(#title,'Share an update')]")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[#contenteditable='true']")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#contenteditable='true']")));
driver.findElement(By.xpath("//div[#contenteditable='true']")).sendKeys("Hello World");
Explanation:
First you have to click on the Share an update text box and then send keys on the div element for which content-editable is set as true.
Related
What is the wrong in my code , why auto click doesn't work in accept cookies button. This website using angular application.
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.paulhammant.ngwebdriver.ByAngular;
import com.paulhammant.ngwebdriver.NgWebDriver;
public class NewClass {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Hp\\Downloads\\chromedriver_win32\\chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
NgWebDriver ngWebDriver = new NgWebDriver(driver);
ngWebDriver.waitForAngularRequestsToFinish();
driver.get("https://visa.vfsglobal.com/ind/en/deu/login/");
ngWebDriver = new NgWebDriver((JavascriptExecutor) driver);
ngWebDriver.waitForAngularRequestsToFinish();
driver.findElement(ByAngular.options("onetrust-accept-btn-handler")).click();
}
}
Attached Image here
When you use this line
driver.findElement(ByAngular.options("onetrust-accept-btn-handler")).click();
it will try to find the element immediately, often resulting in errors. Cause web element/elements have not been rendered properly.
This is main reason we should opt for Explicit waits, implemented by WebDriverWait.
They allow your code to halt program execution, or freeze the thread,
until the condition you pass it resolves. The condition is called with
a certain frequency until the timeout of the wait is elapsed. This
means that for as long as the condition returns a falsy value, it will
keep trying and waiting.
Code :
Webdriver driver = new ChromeDriver();
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.id("onetrust-accept-btn-handler"))).click();
As per my knowledge there is no way to auto accept cookies using ChromeOptions you need to find the element and click.
driver = new ChromeDriver();
driver.get("https://visa.vfsglobal.com/ind/en/deu/login/");
new WebDriverWait(driver, 30).until(ExpectedConditions.elementToBeClickable(By.id("onetrust-accept-btn-handler"))).click();
Please add wait functionality before entering username and password,
if pop up interrupt while entering, username and password will not be proper.
In my case, i created a common method wait and click,
dh.wait_for_the_element_then_do(By.xpath("//button[text()='Accept All Cookies']"), "click", "", "cookies popo up");
dh.wait_for_the_element_then_do(By.xpath("//input[#tcp-auto='input-username']"),"send", username, "login username");
dh.wait_for_the_element_then_do(By.xpath("//input[#tcp-auto='input-password']"),"send", password, "login password");
dh.wait_for_the_element_then_do(By.xpath("//span[text()='Sign in']"),"click", "", "signin button");
This is the solution of my problem.
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.paulhammant.ngwebdriver.ByAngular;
import com.paulhammant.ngwebdriver.NgWebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.By;
public class NewClass {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Hp\\Downloads\\chromedriver_win32\\chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
NgWebDriver ngWebDriver = new NgWebDriver(driver);
ngWebDriver.waitForAngularRequestsToFinish();
driver.get("https://visa.vfsglobal.com/ind/en/deu/login");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions
.elementToBeClickable(By.id("onetrust-accept-btn-handler")))
.click();
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class voot {
public static void main(String[] args) {
// TODO Auto-generated method stub
//WebDriver driver = new SafariDriver();
System.setProperty("webdriver.chrome.driver", "/Users/dkurugod/Desktop/selenium_tutorials/chromedriver");
WebDriver driver = new ChromeDriver();
String URL = "https://voting.voot.com/vote/";
driver.get(URL);
String title = driver.getTitle();
System.out.println(title);
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
WebElement name = driver.findElement(By.xpath("//img[contains(#alt,'Harry')]"));
name.click();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
WebElement submit = driver.findElement(By.xpath("//button[normalize-space()='Submit']"));
submit.click();
}}
<button class="jss190"> Submit</button>
After clicking on Submit button, it is not navigating to the next page. Can someone please suggest to me how to proceed with this. I am still a beginner in Selenium. Thanks
1 You do not need to use this twice:
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
2 For submit button try the following xpath locator:
//button[contains(#class,'jss190')]
Or this:
//button[contains(text(),'Submit')]
The second locator will only work when there in only one button with type submit.
I have written a code to find the number of mails from Gmail inbox.The below code prints 1-100 of 5198.I want the output as only 5198.Can anyone pls help how to get a part of the text as output from a webelement.?
package com.training.edureka.selenium.module4;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class numberOfMails {
public static void main(String[] args) throws InterruptedException {
WebDriver driver;
System.setProperty("webdriver.chrome.driver","C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");
driver = new ChromeDriver();
Thread.sleep(20000);
driver.get("https://www.gmail.com");
driver.manage().window().maximize();
WebElement email = driver.findElement(By.cssSelector("input[type='email']"));
email.sendKeys("ukn#gmail.com");
WebElement next1 = driver.findElement(By.className("CwaK9"));
next1.click();
Thread.sleep(10000);
WebElement pwd = driver.findElement(By.cssSelector("input[type='password']"));
pwd.sendKeys("juiweji123*");
WebElement next2 = driver.findElement(By.cssSelector("span[class='RveJvd snByac']"));
next2.click();
Thread.sleep(20000);
String inbox = driver.findElement(By.id(":ya")).getText();
System.out.println(inbox);
}
}
You can split the text by of
String inbox = driver.findElement(By.id(":ya")).getText();
String[] data = inbox.split("of ");
System.out.println(data[1]);
When I run the test method runSuite(), everything is fine till the last line of the method:
button.click(); // clicks the button on page
The above statement results in the page expired issue.
I get the following message on the browser window:
ERROR: This page has expired. Please cancel this operation and try again.
I tried playing around browser cache, but that didn't help.
package com.swtestacademy.webdriver;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Timeouts;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.junit.*;
import com.util.MockLoginClient;
import static org.junit.Assert.*;
public class MainTest {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
MockLoginClient.instance().authenticateAsHcrCaseWorker();
//System.setProperty("webdriver.ie.driver","C:/IEDriverServer_x64_3.4.0/IEDriverServer.exe");
//driver = new InternetExplorerDriver();
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
FirefoxOptions options = new FirefoxOptions();
options.addPreference("log", "{level: trace}");
capabilities.setCapability("marionette", true);
capabilities.setCapability("moz:firefoxOptions", options);
capabilities.setCapability(CapabilityType.ELEMENT_SCROLL_BEHAVIOR, 1);
capabilities.setCapability(CapabilityType.SUPPORTS_APPLICATION_CACHE, "applicationCacheEnabled");
System.setProperty("webdriver.gecko.driver", "C:/geckodriver-v0.16.1-win64/geckodriver.exe");
//ProfilesIni profile = new ProfilesIni();
//profile.getProfile("default");
FirefoxProfile ffprofile = new FirefoxProfile();
ffprofile.setPreference("browser.cache.disk.enable", true);
ffprofile.setPreference("browser.cache.memory.enable", true);
ffprofile.setPreference("browser.cache.offline.enable", true);
ffprofile.setPreference("network.http.use-cache", true);
driver = new FirefoxDriver(capabilities);//ffprofile);
//driver.manage().deleteAllCookies();
baseUrl = "http://localhost:8082/";
driver.manage().timeouts().implicitlyWait(3000, TimeUnit.MILLISECONDS);
//driver.manage().timeouts().pageLoadTimeout(10,TimeUnit.SECONDS);
// driver.manage().timeouts().setScriptTimeout(3000,TimeUnit.MILLISECONDS);
driver.get(baseUrl + "/Curam/AppController.do");
}
#Test
public void test() throws Exception {
driver.get(baseUrl + "/Curam/AppController.do");
driver.findElement(By.id("app-sections-container-dc_tablist_DefaultAppSection-sbc_tabLabel")).click();
driver.findElement(By.cssSelector("div.dojoxExpandoIcon.dojoxExpandoIconLeft")).click();
Thread.sleep(1000);
driver.findElement(By.id("dijit_layout_AccordionPane_1_button_title")).click();
driver.findElement(By.cssSelector("#dijit_layout_AccordionPane_1 > ul > li > a.curam-content-pane-single-link")).click();
driver.getWindowHandle();
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
iterator.next();
}
//driver.switchTo().window(subWindowHandler); // switch to popup window
driver.findElement(By.id("curam_ModalDialog_0"));
driver.findElement(By.className("dijitDialogPaneContent"));
driver.findElement(By.id("curam_ModalUIMController_0"));
driver.findElement(By.className("contentPanelFrameWrapper"));
driver.switchTo().frame("iframe-curam_ModalDialog_0");
driver.findElement(By.tagName("html"));
driver.findElement(By.tagName("body"));
driver.findElement(By.id("content"));
driver.findElement(By.id("mainForm"));
driver.findElement(By.id("modal-actions-panel"));
// driver.findElement(By.xpath("//div[#id='modal-actions-panel']/div[2]/a/span/span/span")).click();
WebElement nxtBtn = driver.findElement(By.id("__o3btn.NEXTPAGE_2"));
Thread.sleep(1000);
nxtBtn.click();
driver.findElement(By.xpath("//div[#id='modal-actions-panel']/div[2]/a[2]/span/span/span")).click();
driver.findElement(By.id("__o3btn.SAVE_0")).click();
}
#After
public void tearDown() throws Exception {
//driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
#Test
public void runSuite() throws InterruptedException {
System.out.println(driver);
WebElement xy = driver.findElement(By.id("app-sections-container-dc_tablist_DefaultAppSection-sbc_tabLabel"));
xy.click();
driver.findElement(By.cssSelector("div.dojoxExpandoIcon.dojoxExpandoIconLeft")).click();
WebElement personSearchLink = driver.findElement(By.linkText("Person…"));
Thread.sleep(1000);
personSearchLink.click();
WebElement appStc = driver.findElement(By.id("DefaultAppSection-stc"));
WebElement djtTab = appStc.findElement(By.className("dijitTabPaneWrapper"));
Thread.sleep(1000);
WebElement tpChldWrapper = djtTab.findElement(By.className("dijitTabContainerTopChildWrapper"));
WebElement lyoutCntntPane = tpChldWrapper.findElement(By.id("dojox_layout_ContentPane_1"));
WebElement tbWrpr = lyoutCntntPane.findElement(By.className("tab-wrapper"));
WebElement noDtlsPnl = tbWrpr.findElement(By.className("no-details-panel"));
WebElement cntntAreaCntnr = noDtlsPnl.findElement(By.className("content-area-container"));
WebElement fstUIMCntrlr = cntntAreaCntnr.findElement(By.id("curam_FastUIMController_1"));
WebElement cntntPnlFrmWrpr = fstUIMCntrlr.findElement(By.className("contentPanelFrameWrapper"));
WebElement iFrameElmnt = cntntPnlFrmWrpr.findElement(By.tagName("iframe"));
driver.switchTo().frame(iFrameElmnt);
WebElement inputText = driver.findElement(By.id("__o3id0"));
Thread.sleep(100);
inputText.sendKeys("24684");
// Thread.sleep(4000);
WebElement button = driver.findElement(By.linkText("Search"));
button.click();
}
}
I could see the issue. This behavior is seen for Firefox and Internet explorer only and not for chrome.
The solution would be to check the default desired capabilities for Chrome driver and set the same for Firefox driver or IE driver.
Easier Solution: Switch to Chrome Driver.
driver.get("https://www.facebook.com/sachin.aryal");
driver.findElement(By.name("xhpc_message_text")).sendKeys("Testing Java and Selenium");
driver.findElement(By.xpath("//*[#id='u_0_1a']/div/div[6]/div/ul/li[2]/button")).click();
The last line of the code is not working. How do I set the XPath of the Post button on facebook?
I know you already marked your own answer but it's not the proper way to do this. There are ways built into Selenium to do waits, e.g. wait for an element to be clickable. This method is the proper and more efficient way to do this.
driver.get("https://www.facebook.com/sachin-aryal/");
driver.findElement(By.name("xhpc_message_text")).sendKeys("Testing Java and Selenium");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button/span[.=\"Post\"]"))).click();
I noticed it works when you first scroll into view the button, try adding before the click on post the:
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,259)","");
Try the below code .. it must work for you..
import java.util.List;
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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Facebook_login {
public static void main(String[] args) throws InterruptedException {
String user_name = "facebook_user_name";
String pwd = "facebook_password";
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.name("email")).clear();
driver.findElement(By.name("email")).sendKeys(user_name);
driver.findElement(By.name("pass")).clear();
driver.findElement(By.name("pass")).sendKeys(pwd);
driver.findElement(By.xpath("//input[contains(#value,'Log In')]")).click();
Thread.sleep(10000);
System.out.println("logged in successfully");
WebElement notification = driver.findElement(By.xpath("//a[contains(#action,'cancel')]"));
if(notification.isDisplayed()) {
System.out.println("Notification is present");
notification.click();
}
WebElement status =driver.findElement(By.xpath("//textarea[#name='xhpc_message']"));
status.sendKeys("Hello");
Thread.sleep(3000);
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Post']"))).click();
}
}