Selenium Webdriver pass email to form by js - java

How can I pass email into this form?
You need just click "Professional - Start free trial", which is where I need pass email using selenium webdriver.

I noticed that the popup for providing your email is in a different frame. You should manage Context switching:
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Email extends BrowserFunctions{
public static void main(String args[]) throws InterruptedException{
System.setProperty("webdriver.chrome.driver", "Drivers//chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.wrike.com/price/");
Thread.sleep(5000);
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.id("start-free-trial-professional")));
Thread.sleep(5000);
driver.switchTo().defaultContent(); // you are now outside both frames
driver.switchTo().frame("ajaxIframeFeatures");
executor.executeScript("arguments[0].setAttribute('value', 'abc#gmail.com');", driver.findElement(By.id("email")));
executor.executeScript("arguments[0].click();", driver.findElement(By.id("start-project")));
Thread.sleep(5000);
}
}
Sorry for using too much of Thread.sleep(), it's not a good practice generally. You can replace it by WebDriverWait if you want to improve the performance.
I hope it helps :)

Related

Getting NoSuchElementException while trying Datepicker in jquery

Tring to click on the input box, with script able to open the URL-https://jqueryui.com/datepicker/.In locator the id is available still getting No element foun.
Tried with thread.sleep as well
when i am running the script getting exception
package SeleniumWebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class HandlingCalender {
public static void main(String[] args) throws InterruptedException {
WebDriver driver =new ChromeDriver();
driver.get("https://jqueryui.com/datepicker/");
//driver.manage().window().maximize();
Thread.sleep(1000);
driver.findElement(By.id("datepicker")).click();
Element you trying to access is inside an iframe.
So, to access it you need first to switch into that iframe, as following:
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.class("demo-frame")));
wait.until(ExpectedConditions.elementToBeClickable(By.id("datepicker"))).click();

How to perform multiple actions and click on the link with text as Member Login on the url http://www.spicejet.com/ through selenium-webdriver

I tried the below code but it is not mouse hovering and clicking on 'Member login'
WebElement lgn = driver.findElement(By.id("ctl00_HyperLinkLogin"));
WebElement ssm = driver.findElement(By.xpath("//a[contains(text(), 'SpiceCash/SpiceClub Members')]"));
WebElement cgm = driver.findElement(By.xpath("//a[contains(text(),'Member Login')]"));
Actions a1 = new Actions(driver);
a1.moveToElement(lgn).moveToElement(ssm).moveToElement(cgm).click().build().perform();
To invoke click() on the element with text as Member login, first you have to Mouse Hover over the element with text as LOGIN / SIGNUP, then Mouse Hover over the element with text as SpiceCash/SpiceClub Members then induce WebDriverWait for the element with text as Member Login to be clickable and you can use the following solution:
Code Block:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Spicejet_member_login {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.spicejet.com/");
new Actions(driver).moveToElement(new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a.link#ctl00_HyperLinkLogin")))).build().perform();
new Actions(driver).moveToElement(new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[#class='hide-mobile']/a[contains(.,'SpiceCash/SpiceClub Members')]")))).build().perform();
new WebDriverWait(driver, 7).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[#class='hide-mobile']//ul/li/a[#href='https://book.spicejet.com/Login.aspx' and contains(.,'Member Login')]"))).click();
}
}
Browser Snapshot:
You can try to add waits between your moveToElement() calls
WebDriverWait wait = new WebDriverWait(WebDriverRunner.getWebDriver(), 10);
wait.until(ExpectedConditions.visibilityOf(element))
where the "element" is your menu that should appear on hover.
Or you can use ready solution Selenide framework which is built on top of the Selenium and has built in hover method and waits which help to handle page dynamics
By this link you can find an example of hover() method usage.

Industry Grade Selenium Webdrive Template

I've recently started learning about selenium WebDriver, and I've learned a lot of stuff from different sources but I don't have a good idea of how should a clean/professional grade script should look like and how should it's content be written.
This is an example of a login that I've created as a test, what could I change?
package Facebook;
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 Login {
WebDriver driver = new ChromeDriver();
public void login() throws InterruptedException
{
driver.get("http://www.facebook.com");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement user = driver.findElement(By.id("email"));
WebElement password = driver.findElement(By.id("pass"));
user.sendKeys("user_test");
password.sendKeys("password_test");
Thread.sleep(3000);
user.clear();
password.clear();
WebElement submit = driver.findElement(By.id("u_0_u"));
if(submit.isDisplayed())
{
System.out.println("\u001B31;1m Succes");
}
else
{
System.out.println("\u001B31;2m Fail");
}
}
public static void main(String[] args) throws InterruptedException {
Login obj = new Login();
obj.login();
}
}
You should spend some time learning about the Page Object Model. If you are going to build more than a few tests, it will be a significant boost to organization, keeping your code clean, and lessening the maintenance burden.
Avoid Thread.sleep() and implicit waits. Instead prefer WebDriverWait.
Don't write your own logging/reporting. Instead use JUnit or TestNG. They are well established and will save you a lot of time with not only logging but handling organization of your tests, executions, reporting, etc.
NOTE: Be careful about questions on SO that sound like asking for a code review. There's a whole other site for that, http://codereview.stackexchange.com.

Selenium WebDriver -- XPath

Trying to get the script to select the filter button on the top but can't seem to figure out how to input the XPath. I believe it has something to do with it be in a separate iframe.
package chromebrowser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class JavaClass {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Newfolder\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://mlvtwrew73.consilio.com/Relativity/");
driver.manage().window().maximize();
//Thread.sleep(5000); this can be used as a wait command before moving on to the next function
WebElement objWE;
Thread.sleep(9000);
// objWE = driver.findElement(By.linkText("User Status"));
// objWE.click();
driver.switchTo().defaultContent();
driver.findElement(By.xpath("id(\"ctl00_ctl00_itemList_FilterSwitch\")")).click();
// objWE = driver.findElement(By.id("1"));
// driver.close(); will be used to close the site once all testing completes
}
}
Use ID locator -- it's more appropriate here (and faster than XPath):
WebDriverWait wait= new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.id("ct‌l00_ctl00_itemList_F‌​ilterSwitch")));
driver.findElement(By.id("ctl00_ctl00_itemList_FilterSwitch")).click();
Looks like you're passing in an incorrect XPath. Try this one:
driver.findElement(By.xpath("//a[#id='ctl00_ctl00_itemList_FilterSwitch']")).click();
If there is then why you are using xpath.
Use id like this
driver.findElment(By.id("ctl00_ctl00_itemList_FilterSwitch"). click ();
I need more clarity regarding your question.
As I have understand, I think you need a requirement of XPath of the Filters image. If i am right, try this:
//div[#class='actionCellContainer']//a/img[#class='itemListActionImage']

How to set focus on element on moving the mouse over it in Selenium WebDriver using Java?

I am a beginner in Selenium WebDriver and I'm using this test on the StackOverflow homepage. These will be the steps of my test:
Firstly, go to the homepage of the StackOverflow.
Then, move the mouse on the Users button such that it will be focused. (We don't have to click on it just hover the mouse over it.)
Now, find the active element on the screen (i.e. the element which is
currently focused) and then click on it.
I want the Users button to be clicked because it is focussed currently but that's not happening. Instead the Questions button is clicked on.
Here is my code for the same test.
package insertCartridge;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Practice {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver", "D:\\SELENIUM\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
String baseUrl = "https://stackoverflow.com/";
driver.get(baseUrl);
driver.manage().window().setSize(new Dimension(1920, 1080));
WebElement Users = driver.findElement(By.xpath("/html/body/header/div/div[1]/nav/ol/li[5]"));
Thread.sleep(3000);
Actions builder = new Actions(driver);
builder.moveToElement(Users).perform();
Thread.sleep(3000);
driver.switchTo().activeElement().click();
}
}
I am not able to understand why I am not getting the expected output. Please help.
I don't know why Actions class is not focusing the element. Its an issue or something as so many time i have trouble with Actions class in Firefox browser.
Still you have one alternative way to Focus on desired element and then perform click on focused element i.e. JavascriptExecutor
Here you is your code :
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('nav-users').focus();");
System.out.println(driver.switchTo().activeElement().getTagName());
driver.switchTo().activeElement().click();

Categories