Java Selenium Web Driver 无法定位元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20461088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:06:00  来源:igfitidea点击:

Selenium Web Driver is unable to locate element

javaseleniumwebdriverselenium-webdriver

提问by user3081114

I've been trying to create a small program to put items into a cart. Its supposed to go the page where the item is located and add it to the cart. Then, all the billing information would be input by using the data in a different java class. Every time I run this code:

我一直在尝试创建一个小程序来将物品放入购物车。它应该转到该项目所在的页面并将其添加到购物车。然后,将使用不同 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();
    }
}

I get this error:

我收到此错误:

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

Thanks for the help!

谢谢您的帮助!

回答by Yi Zeng

Looks like timing issue to me. After you redirect to checkout, you might want to wait for the elements before interacting. See Explicit Waitsin documentation.

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

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

回答by fahad

you must check is this element in and IFRAME if yes then first switch into iframe and secondly if by ID is not working then Use Xpath or CSS path can you please share HTML source with me.

您必须检查此元素是否在 IFRAME 中,如果是,则首先切换到 iframe,其次,如果 ID 不起作用,则使用 Xpath 或 CSS 路径,请与我分享 HTML 源代码。