I am attempting to fill the field on this website that asks for credit card, however selenium it not allowing me to send keys into the field. I believe this is because you must initially click on the field, but for some reason I can not get it to do that either. Does anyone have any insight?
Website: https://givingday.northeastern.edu/pages/giving-page-2
>
Under "Club Sports" click "Give", then click "Archery", then click "Next"
to get to CC field
package com.demo.testcases;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
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;
import org.openqa.selenium.support.ui.Select;
public class StackOverFlow{
static WebDriver driver;
static WebDriverWait wait;
public static void main(String[] args) throws InterruptedException {
driver = new ChromeDriver();
driver.manage().window().maximize();
wait = new WebDriverWait(driver, 40);
driver.get("https://givingday.northeastern.edu/pages/giving-page-2");
Thread.sleep(1000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".campaign-tiles-content")));
scrollDown(driver, "scroll(0,500)");
Thread.sleep(1000);
driver.findElement(By.xpath("//a[text()='Club Sports']/parent::div/following-sibling::div[#class='inline-b']"
+ "/descendant::button")).click();
Thread.sleep(1000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".giving-form-billing")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//h3[text()='Archery']")));
Thread.sleep(1000);
driver.findElement(By.xpath("//button[text()='load more causes']")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//h3[text()='Archery']")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//div[#class='flex-it']/descendant::input")).clear();
driver.findElement(By.xpath("//div[#class='flex-it']/descendant::input")).sendKeys("1");
Thread.sleep(1000);
driver.findElement(By.xpath("//button[text()='Next']")).click();
}
public static void scrollDown(WebDriver driver, String YoffSet){
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript(YoffSet);
}
}
There is a frame on this credit card input. You should first switch to frame then you can send a key.
driver.switchTo().frame(driver.findElement(By.id("spreedly-number-frame-1398")));
driver.findElement(By.id("card_number")).sendKeys(keysTosend);
public void enterCardDetails(String cardNumber) throws Throwable {
System.out.println(cardNumber.length());// it will print your card length
for (int i = 0; i < cardNumber.length(); i++) {
char c = cardNumber.charAt(i);
String s = new StringBuilder().append(c).toString();
driver.findElement(By.xpath("//*[#id='ccard_number']")).sendKeys(s);
}
}
Note:
You have to define and call cardnumber as per your requirement.
Related
I'm testing this on my local system and trying to verify user is successfully logged in and landed on correct page.
Getting error when trying to compare String data with webelement data.
`package wnsautomation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//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 login {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "C:\\Users\\orange\\Downloads\\geckodriver.exe");
driver= new FirefoxDriver();
WebDriverWait myWait = new WebDriverWait(driver, 10);
String baseUrl = "http://192.168.1.52:9000";
driver.get(baseUrl);
myWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[2]/div/div/input")));
driver.findElement(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[2]/div/div/input")).sendKeys("admin#gmail.com");
driver.findElement(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[3]/div/div/input")).sendKeys("8JXzwRs4VWeGP0Sy");
driver.findElement(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[5]/button")).click();
String expectedtext="Summary";
WebElement actualtext;
actualtext = driver.findElement(By.linkText("/html/body/div[3]/div/ng-include/div/div/div[1]/div/h3"));
if (actualtext.contentEquals(expectedtext)){
System.out.println("User succesfully loggedIN");
} else {
System.out.println("Invalid credtendials!!");
}
}
}`
Instead of using Webelement, use String with getText()method so, it will give you the actual String. Not the Webelement.
Note: Instead of using absolute xpath, use relative xpath. For more details on xpath tutorial, refer this link.
String expectedtext="Summary";
String actualtext = driver.findElement(By.xpath("//div/h3[text()='Summary']")).getText();
System.out.println(actualtext);
if (expectedtext.equalsIgnoreCase(actualtext))
{
System.out.println("User succesfully loggedIN");
}
else
{
System.out.println("Invalid credtendials!!");
}
Try this-
String expectedtext="Summary";
String actualtext= null;
actualtext = driver.findElement(By.xpath("/html/body/div[3]/div/ng-include/div/div/div[1]/div/h3")).gettext();
I am getting the error message:
NoSuchElementException: no such element: Unable to locate element:
{"method":"xpath","selector":"html/body/form/input[1]"}
when I try running the code below.
And the xpath is correct I already double checked
package Package;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
//import org.openqa.selenium.firefox.FirefoxDriver;
public class Selenium1 {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.w3schools.com/Html/tryit.asp?filename=tryhtml_checkbox");
WebElement ele =driver.findElement(By.xpath("html/body/form/input[1]"));
boolean displayedstatus = ele.isDisplayed();
System.out.println("The display status :"+displayedstatus);
boolean enablestatus = ele.isEnabled();
System.out.println("The enable status :"+enablestatus);
boolean selectedstatus = ele.isSelected();
System.out.println("The selected status :"+selectedstatus);
ele.click();
selectedstatus = ele.isSelected();
System.out.println("The selected status :"+selectedstatus);
}
}
Whenever you try searching for the elements within the iframe you must have to switch the focus to the iframe that you are dealing with.
Try this before searching for the elements within the iframe:
driver.switchTo().frame(driver.findElement(By.name("iframeTitle")));
In this case, the iframe Title is : iframeResult
Below is the code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
//import org.openqa.selenium.firefox.FirefoxDriver;
public class Selenium1 {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.w3schools.com/Html/tryit.asp?filename=tryhtml_checkbox");
//switching focus to iframe
driver.switchTo().frame(driver.findElement(By.name("iframeResult")));
WebElement ele =driver.findElement(By.xpath("html/body/form/input[1]"));
boolean displayedstatus = ele.isDisplayed();
System.out.println("The display status :"+displayedstatus);
boolean enablestatus = ele.isEnabled();
System.out.println("The enable status :"+enablestatus);
boolean selectedstatus = ele.isSelected();
System.out.println("The selected status :"+selectedstatus);
ele.click();
selectedstatus = ele.isSelected();
System.out.println("The selected status :"+selectedstatus);
}
}
If you want to handle one of two checkboxes, you need to switch to iframe first and then search for the element.
driver.get("https://www.w3schools.com/Html/tryit.asp?filename=tryhtml_checkbox");
driver.switchTo().frame("iframeResult");
WebElement ele =driver.findElement(By.xpath("//input[#value='Bike']"));
If after that you want to handle elements outside iframe, you might need to switch back with
driver.switchTo().defaultContent();
I tried this exact code on other websites and it seems to work fine. It's just on pizza hut that it can't even locate an element let alone click on it. Thread.sleep() doesn't make a difference. The problem is between the commented *, according to the compiler. Here's the code.
package training;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class PizzaHut {
WebDriver driver;
#Test
public void open() throws Exception {
//SET UP WEBDRIVER AND OPEN WEBSITE
System.setProperty("webdriver.chrome.driver","/Users/1mr4n/Downloads/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.pizzahut.com/#/home");
Thread.sleep(1000);
//CLICK PIZZA
Thread.sleep(5000);
test("before");
//**************PROBLEM*CODE**********************
driver.findElement(By.xpath("//a[#id='lg-nav-pizza']")).click();
//************************************************
test("clicked pizza");
//CLOSE BROWSER
Thread.sleep(15000);
driver.close();
}
public static void test(String x) {
System.out.println(x);
}
}
I just ran the code below and it worked just fine. No need for sleep or waits and I'm using Chrome also.
driver.get("https://www.pizzahut.com");
driver.findElement(By.id("lg-nav-pizza")).click();
Try this one ,definitely it help you.
package training;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class PizzaHut {
WebDriver driver;
#Test
public void open() throws Exception {
//SET UP WEBDRIVER AND OPEN WEBSITE
// System.setProperty("webdriver.chrome.driver","/Users/1mr4n/Downloads/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.pizzahut.com/#/home");
Thread.sleep(1000);
//CLICK PIZZA
Thread.sleep(5000);
test("before");
List<WebElement> webElements=driver.findElement(By.cssSelector("div.btn-group.btn-group-lg.btn-group-justified.ph-header-navigation"))
.findElements(By.cssSelector("div.btn.btn-link.ph-ghost-padding.ng-scope"));
for (WebElement element : webElements) {
if (element.getAttribute("title").equals("PIZZA")) {
element.click();
}
}
Thread.sleep(5000);
test("clicked pizza");
//CLOSE BROWSER
Thread.sleep(15000);
driver.close();
}
public static void test(String x) {
System.out.println(x);
}
}
You are not able to click it because it is an AngularJS tag, hence it takes some time to load: add some wait like below and it works (Here i'm just adding thread.sleep for the sake of simplicity,I've increased it to 15000ms):
//CLICK PIZZA
Thread.sleep(15000);
test("before");
//**************PROBLEM*CODE**********************
driver.findElement(By.xpath("//a[#id='lg-nav-pizza']")).click();
I just started learning selenium WD. I am using firefox, & java 1.8 & eclipse in my mac. In the following code, I am trying to print the lowest price value from
orbitz.com web site.
When I get to wait.until toward the end of the code, it gets stuck & does not return. I can not figure out why & need some help resolving this problem.
package fh2_orbitzWD;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class WDDemo1 {
public static void main(String[] args) throws InterruptedException,
IOException{
WebDriver wd = new FirefoxDriver();
wd.get("http://www.orbitz.com");
wd.findElement(By.xpath("//input[#name='search.type']")).click();
//Thread.sleep(1000);
WebDriverWait wait = new WebDriverWait(wd,60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name
("ar.rt.leaveSlice.orig.key")));
wd.findElement(By.name("ar.rt.leaveSlice.orig.key")).clear();
wd.findElement(By.name("ar.rt.leaveSlice.orig.key")).sendKeys("DFW");
wd.findElement(By.name("ar.rt.leaveSlice.dest.key")).clear();
wd.findElement(By.name("ar.rt.leaveSlice.dest.key")).sendKeys("SFO");
wd.findElement(By.name("ar.rt.leaveSlice.date")).clear();
wd.findElement(By.name("ar.rt.leaveSlice.date")).sendKeys("5/15/2015");
wd.findElement(By.name("ar.rt.returnSlice.date")).clear();
wd.findElement(By.name("ar.rt.returnSlice.date")).sendKeys("5/25/2015");
System.out.println("Right before the click of search");
wd.findElement(By.name("search")).click();
WebDriverWait wait2 = new WebDriverWait(wd,80);
wait2.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*
[#id='resultsTwoColumn']")));***
String price = wd.findElement(By.xpath("//span[#class='money small-
cents']")).getText();
System.out.println("Price is: " + price);
}
}
I want to be able to scroll the subscriptions list of my personal YouTube page, how do I do this? I have written code that allows me to scroll the main page, any ideas as to how the code could be tweaked to scroll the "My subscriptions" section of the YouTube page that appears when signed into YouTube?
package Check;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class java {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://www.youtube.com");
Thread.sleep(2500);
driver.findElement(By.xpath("//button[contains(.,'Sign in')]")).click();
Thread.sleep(1500);
driver.findElementById("Email").sendKeys("<My username>");
driver.findElementById("Passwd").sendKeys("<My password>");
driver.findElementById("signIn").click();
//driver.findElement(By.xpath("//button[contains(.,'Sign in')]")).click();
Thread.sleep(3500);
driver.findElementByCssSelector("div[id='identity-prompt-account-list'] > ul > label + label").click();
driver.findElementById("identity-prompt-confirm-button").click();
Thread.sleep(2500);
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight," +
"document.body.scrollHeight,document.documentElement.clientHeight));");
Thread.sleep(5000);
driver.quit();
}
}
I have found by a method of trial and error a method that works, by the CSS selector appears to be limited to finding up to the 35th sibling so in the light of this limitation here is the code that I came up with this works very well for what I want to accomplish. Here is my script below:
package Check;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.firefox.FirefoxDriver;
public class java2 {
public static void main(String[] args) throws InterruptedException {
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://www.youtube.com");
Thread.sleep(2500);
driver.findElement(By.xpath("//button[contains(.,'Sign in')]")).click();
Thread.sleep(1500);
driver.findElementById("Email").sendKeys("<User name>");
driver.findElementById("Passwd").sendKeys("<Password>");
driver.findElementById("signIn").click();
Thread.sleep(4000);
driver.findElementByCssSelector("div[id='identity-prompt-account-list'] > ul > label + label").click();
driver.findElementById("identity-prompt-confirm-button").click();
Thread.sleep(3000);
JavascriptExecutor js = (JavascriptExecutor)driver;
int i =0;
String CSSText = "ul[id='guide-channels'] > li";
do {
if (driver.findElementByCssSelector(CSSText).getText().equals("Josie Outlaw")){
break;
}
CSSText = CSSText + " + li";
i++;
} while (i<35);
js.executeScript("document.getElementsByClassName(\"vve-check overflowable-list-item guide-channel\")["+i+"].scrollIntoView(false);");
Thread.sleep(1500);
js.executeScript("document.getElementsByClassName(\"vve-check overflowable-list-item guide-channel\")["+i+"].scrollIntoView(true);");
Thread.sleep(10000);
driver.quit();
}
}