使用Selenium Java无法访问元素的xPath

时间:2018-04-12 05:10:37

标签: java selenium selenium-webdriver webdriver

我基本上已经尝试了所有可能,但我找不到将密钥发送到以下信用卡字段的方法。我相信这是因为首先需要点击该字段,但我甚至无法选择该字段来点击它。非常感谢任何帮助。

网站链接:https://givingday.northeastern.edu/pages/giving-page-2

点击字段之前的HTML:

<form accept-charset="UTF-8" action="javascript:void(0);" id="number-form" onsubmit="return false;">
<label for="card_number" class="visuallyhidden" id="number_label">Card Number</label>
<input type="text" pattern="[0-9]*" name="card_number" id="card_number" autocomplete="off" size="19" style="width: 100%; height: 2.5em; border-radius: 2px; border: 1px solid rgb(226, 226, 226); font-family: Arial, &quot;Helvetica Neue&quot;, Helvetica, sans-serif; padding-left: 5%; font-size: 14px; color: rgb(51, 51, 51); box-sizing: border-box;">

点击字段后的HTML:

<form accept-charset="UTF-8" action="javascript:void(0);" id="number-form" onsubmit="return false;" _lpchecked="1">
<label for="card_number" class="visuallyhidden" id="number_label">Card Number</label>
<input type="text" pattern="[0-9]*" name="card_number" id="card_number" autocomplete="off" size="19" style="width: 100%; height: 2.5em; border-radius: 2px; border: 1px solid rgb(226, 226, 226); font-family: Arial, &quot;Helvetica Neue&quot;, Helvetica, sans-serif; padding-left: 5%; font-size: 14px; color: rgb(51, 51, 51); box-sizing: border-box;">

<pre id="_h#2" style="white-space: pre-wrap; position: absolute; z-index: -9; visibility: hidden; display: block; font-family: Arial, &quot;Helvetica Neue&quot;, Helvetica, sans-serif; font-size: 14px; font-weight: 400; font-style: normal; text-transform: none; text-decoration: none solid rgb(51, 51, 51); letter-spacing: normal; word-spacing: 0px; line-height: normal; text-align: start; vertical-align: baseline; direction: ltr; width: 335px; height: 35px; margin: 0px; padding: 1px 1px 1px 16.75px; border-width: 1px; border-style: solid; overflow: auto; left: 0px; top: 0px;"><span>​</span> </pre>

3 个答案:

答案 0 :(得分:0)

根据我的假设并以最简单的最不可重复使用的方式,您将需要:

driver.switchTo.frame('spreedly-number-frame-9064')
driver.findElement(by.id('card_number')).click

答案 1 :(得分:0)

信用卡 CVV 号码字段位于 iframe 中。在打开的提示符下有 2 iframe

建议:您必须切换到iframe才能在其中执行某些操作。

以下是解决您问题的代码:(Java + Selenium)

public class StackOverFlow {

    static WebDriver driver;
    static WebDriverWait wait;

    public static void main(String[] args) throws InterruptedException {


            System.setProperty("webdriver.chrome.driver", "F:\\Automation_Learning\\chromedriver.exe");
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            wait = new WebDriverWait(driver, 40);
            driver.get("https://givingday.northeastern.edu/pages/giving-page-2");
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".campaign-tiles-content")));
            Thread.sleep(5000);
            scrollDown(driver, "scroll(0,700)");
            driver.findElement(By.xpath("//a[text()='Club Sports']/parent::div/following-sibling::div[@class='inline-b']/descendant::button")).click();
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".giving-form-billing")));
            wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//h3[text()='Archery']")));
            driver.findElement(By.xpath("//h3[text()='Archery']")).click();
            scrollDown(driver, "scroll(0,500)");
            wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[class$='secondary-color-background']")));
            driver.findElement(By.cssSelector("button[class$='secondary-color-background']")).click();
            wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("span[class^='step_credit']"))));
            driver.findElement(By.name("first_name")).sendKeys("abc");
            driver.findElement(By.name("last_name")).sendKeys("xyz");
            driver.findElement(By.name("email")).sendKeys("abcxyz@hotmail.com");
            driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[id^='spreedly-number-frame']")));
            Thread.sleep(5000);
            driver.findElement(By.xpath("//form[@id='number-form']/descendant::input")).sendKeys("12345678");
        }

        public static void scrollDown(WebDriver driver, String YoffSet){
            JavascriptExecutor jse = (JavascriptExecutor)driver;
            jse.executeScript(YoffSet);
        }
}

注意::我正面临互联网问题,这就是我使用Thread.sleep(5000)的原因;

如果需要,您也可以使用显式等待。

如果您对此有任何疑虑,请与我们联系。

答案 2 :(得分:-1)

试试这个希望这会有所帮助, 。driver.switchTo()帧(0); wait.until(ExpectedConditions.visibilityOfElementLocated((By.xpath(&#34; .//* [@ ID =&#39; CARD_NUMBER&#39;]&#34;))));  ele.sendKeys(&#34;卡号和#34;);