【问题标题】:Entering Credit Card Number Java Selenium输入信用卡号 Java Selenium
【发布时间】:2018-09-21 22:09:35
【问题描述】:

我正在尝试填写该网站上要求信用卡的字段,但是 selenium 不允许我将密钥发送到该字段。我相信这是因为您必须首先单击该字段,但由于某种原因,我也无法让它这样做。有没有人有任何见解?

网址:https://givingday.northeastern.edu/pages/giving-page-2

> 在“俱乐部体育”下单击“给予”,然后单击“射箭”,然后单击“下一步” 进入 CC 字段

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

【问题讨论】:

标签: java selenium selenium-webdriver webdriver


【解决方案1】:

此信用卡输入上有一个框架。您应该首先切换到帧,然后您可以发送密钥。

driver.switchTo().frame(driver.findElement(By.id("spreedly-number-frame-1398")));

driver.findElement(By.id("card_number")).sendKeys(keysTosend);

【讨论】:

  • 我已经尝试过了,但它不起作用。框架 ID/名称中的 4 位数字是动态的。
  • Club Sports 下没有给予按钮。你能给我一个相关领域的确切页面或源代码的链接(包含框架和信用卡标签)。我们应该使用另一个选择器访问框架,即:xpath 或 css
【解决方案2】:
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); 
    }
}

注意: 您必须根据您的要求定义并调用cardnumber

【讨论】:

    猜你喜欢
    • 2021-08-06
    • 2012-02-22
    • 2021-09-15
    • 2018-10-12
    • 2021-03-15
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    • 2011-02-24
    相关资源
    最近更新 更多