java - Selenium Web 驱动程序无法定位元素

标签 java selenium webdriver selenium-webdriver

我一直在尝试创建一个小程序来将商品放入购物车。它应该转到商品所在的页面并将其添加到购物车。然后,将使用不同 java 类中的数据输入所有帐单信息。每次我运行这段代码时:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

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.Select;
import org.openqa.selenium.support.ui.WebDriverWait;



public class Supreme {

    public static void main(String[] args) throws Exception{
        long start = System.nanoTime();
        WebDriver driver = new FirefoxDriver();

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        driver.get("http://www.supremenewyork.com/shop/hats/selassie-beanie/grey");
        WebElement add = driver.findElement(By.name("commit"));
        add.click();

        driver.get("https://www.supremenewyork.com/checkout");

        AccountInfo a = new AccountInfo();
        a = a.getAccount();

        WebElement name = driver.findElement(By.id("order_billing_name"));
        name.sendKeys(a.getName());

        WebElement email = driver.findElement(By.id("order_email"));
        email.sendKeys(a.getEmail());

        WebElement phone = driver.findElement(By.id("order_tel"));
        phone.sendKeys(a.getPhone());

        WebElement address1 = driver.findElement(By.id("order_billing_address"));
        address1.sendKeys(a.getAddress1());

        WebElement address2 = driver.findElement(By.id("order_billing_address_2"));
        address2.sendKeys(a.getAddress2());

        WebElement city = driver.findElement(By.id("order_billing_city"));
        city.sendKeys(a.getCity());

        WebElement zip = driver.findElement(By.id("order_billing_zip"));
        zip.sendKeys(a.getZip());

        Select state = new Select(driver.findElement(By.id("order_billing_state")));
        state.selectByVisibleText(a.getState());

        Select type = new Select(driver.findElement(By.id("credit_card_type")));
        type.selectByVisibleText(a.getType());

        WebElement credit = driver.findElement(By.id("credit_card_number"));
        credit.sendKeys(a.getCredit());

        Select creditmonth = new Select(driver.findElement(By.id("credit_card_month")));
        creditmonth.selectByVisibleText(a.getExpMonth());

        Select credityear = new Select(driver.findElement(By.id("credit_card_year")));
        credityear.selectByVisibleText(a.getExpYear());

        WebElement cvv = driver.findElement(By.id("credit_card_verification_value"));
        cvv.sendKeys(a.getCVV());

        List<WebElement> check = driver.findElements(By.className("iCheck-helper"));
        for(WebElement w : check){
            w.click();
        }

        WebElement process = driver.findElement(By.name("commit"));
        process.click();
    }
}

我收到这个错误:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"order_billing_name"}

感谢您的帮助!

最佳答案

对我来说似乎是时间问题。重定向到结帐后,您可能希望在交互之前等待元素。参见 Explicit Waits在文档中。

WebDriverWait wait = new WebDriverWait(driver, 60);// 1 minute 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("order_billing_name")));
driver.findElement(By.id("order_billing_name")).sendKeys(a.getName());

关于java - Selenium Web 驱动程序无法定位元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20461088/

相关文章:

Java:更改 jLabel 前景色

java - 将字符更改为字符串

java - 什么是需要 UTF-16 代理项对的好的 Unicode 代码点?

ruby - selenium webdriver 异常

java - 无法使用给定的 URL 打开 Firefox 浏览器 - Selenium Webdriver 和 Java

java - `URLEncoder.encode` 的正确编码

selenium - 如何在 selenium 3 中执行 webdriver 支持的 selenium?

java - 如何重置字段中的用户名?

webdriver - ChromeDriver 控制台应用程序隐藏

c# - Selenium 网络驱动程序 : how to deal with javascript onclick in C#