Java Selenium Webdriver findelement i sendKeys()不是';I don’我没有表现得像预期的那样

Java Selenium Webdriver findelement i sendKeys()不是';I don’我没有表现得像预期的那样,java,xpath,selenium-webdriver,Java,Xpath,Selenium Webdriver,我试图在SeleniumWebDriver中使用下面的xpath和name值来识别Firefox中的元素,但它不起作用。在我看来,这个页面上的所有输入字段都很奇怪,我不知道如何填充它们 driver.findElement(By.name("sender-postCode")).sendKeys("02791"); driver.findElement(By.xpath(".//*[@id='stepOneForm']/div[2]/div[4]/div[1]/div[1]/div[1]/inp

我试图在SeleniumWebDriver中使用下面的xpath和name值来识别Firefox中的元素,但它不起作用。在我看来,这个页面上的所有输入字段都很奇怪,我不知道如何填充它们

driver.findElement(By.name("sender-postCode")).sendKeys("02791");
driver.findElement(By.xpath(".//*[@id='stepOneForm']/div[2]/div[4]/div[1]/div[1]/div[1]/input")).sendKeys("02791");
这是我的代码:

package SalesBar;
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.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
public class Salesbar {
public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    System.setProperty("webdriver.gecko.driver", "C:/Users/User/Documents/SeleniumWebDriver/geckodriver.exe");
    ProfilesIni profil = new ProfilesIni();
    FirefoxProfile myprofile = profil.getProfile("default");

    WebDriver driver;
    driver = new FirefoxDriver(myprofile);

    driver.get("https://wwwuat.dpdpickup.pl/Wycen-i-nadaj-Online");
    driver.findElement(By.xpath(".//*[@id='contentWrapper']/div/div/div[2]/div[1]/a")).click();
    Thread.sleep(3000);
    driver.findElement(By.xpath(".//*[@id='stepOneForm']/div[2]/div[3]/span[2]/label/span")).click();
    Thread.sleep(3000);
    driver.findElement(By.name("parcels-1-weight")).sendKeys("5");
    }

请告诉我WebDriver中是否有标准的方法来查找和填写这些字段。

所有测试自动化工具的一个问题是,当自动化工具执行时,页面可能尚未完成加载。有不同的复杂程度,可以用来使这是100%可靠。第一道防线是使用。第一个例子

WebDriver webDrive = ... // You have to initialize to a browser instance and navigate to your web page
By bySenderPostCode = By.name("sender-postCode");
Wait<WebDriver> wait_element = new WebDriverWait(webDriver, 40); // Wait up to 40 seconds
WebElement senderPostalCodeElement = wait_element.until(ExpectedConditions.visibilityOfElementLocated(bySenderPostCode));
senderPostalCodeElement.sendKeys("02791");
必须针对您必须在其中操作的环境对这些进行调整。等待jQuery完成与等待AngularJs不同。但我给你的应该让你走。让我知道结果如何

编辑

我意识到告诉你我用来等待但不共享代码的例程是毫无意义的。加载微调器完全取决于实现。没有一种方法可以保证在任何地方都能工作,但我给了您一种通用的形式,这是AngularJs实现的常见形式

以下是其他人:

public void waitForBrowserReadystateComplete(WebDriver webDriver) {
    for (int a=0; a<20; a++) {
        JavascriptExecutor javascriptExecutor = (JavascriptExecutor) webDriver;
        if (javascriptExecutor.executeScript("return document.readyState")
                .toString().equals("complete")) {
            break;
        }
        sleepResponsibly(500);
    }
}

public void sleepResponsibly(int timeMillisecond){
    try{
        Thread.sleep(timeMillisecond);
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt(); 
        throw new RuntimeException(ex);
    }
}

public boolean waitForAngularToLoad(WebDriver driver, int timeout) {
    driver.manage().timeouts().setScriptTimeout(timeout, TimeUnit.SECONDS);
    WebDriverWait wait = new WebDriverWait(driver, timeout, 500L);
    return wait.until(angularHasFinishedProcessing());
}


public static ExpectedCondition<Boolean> angularHasFinishedProcessing() {
    return new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            String hasAngularFinishedScript = "var callback = arguments[arguments.length - 1];\n" +
                    "var el = document.querySelector('html');\n" +
                    "if (!window.angular) {\n" +
                    "    callback('false')\n" +
                    "}\n" +
                    "if (angular.getTestability) {\n" +
                    "    angular.getTestability(el).whenStable(function(){callback('true')});\n" +
                    "} else {\n" +
                    "    if (!angular.element(el).injector()) {\n" +
                    "        callback('false')\n" +
                    "    }\n" +
                    "    var browser = angular.element(el).injector().get('$browser');\n" +
                    "    browser.notifyWhenNoOutstandingRequests(function(){callback('true')});\n" +
                    "}";

            JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
            String isProcessingFinished = javascriptExecutor.executeAsyncScript(hasAngularFinishedScript).toString();

            return Boolean.valueOf(isProcessingFinished);
        }
    };
}

嗨,Mike,据我所知,应用程序使用的是jquery而不是AngularJs。所以我用了你的第一个例子。但它不起作用。你打开我附加的链接了吗?当你点击收音机按钮时,你会看到表格。我改变了
Wait-Wait\u元素=新的WebDriverWait(webDriver,40)
Wait Wait_element=new WebDriverWait(驱动程序,40)
;因为它被标记(webDriver无法解析为变量),所以您可以更具体地说明“未工作”吗?另外,请发布您执行的更新代码。请注意。所以我使用了selenium java v2.53.1和firefox v47.0。我已经切换到chrome webdriver和selenium java v2.53,这解决了我的问题。谢谢你,迈克。
public void waitForBrowserReadystateComplete(WebDriver webDriver) {
    for (int a=0; a<20; a++) {
        JavascriptExecutor javascriptExecutor = (JavascriptExecutor) webDriver;
        if (javascriptExecutor.executeScript("return document.readyState")
                .toString().equals("complete")) {
            break;
        }
        sleepResponsibly(500);
    }
}

public void sleepResponsibly(int timeMillisecond){
    try{
        Thread.sleep(timeMillisecond);
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt(); 
        throw new RuntimeException(ex);
    }
}

public boolean waitForAngularToLoad(WebDriver driver, int timeout) {
    driver.manage().timeouts().setScriptTimeout(timeout, TimeUnit.SECONDS);
    WebDriverWait wait = new WebDriverWait(driver, timeout, 500L);
    return wait.until(angularHasFinishedProcessing());
}


public static ExpectedCondition<Boolean> angularHasFinishedProcessing() {
    return new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            String hasAngularFinishedScript = "var callback = arguments[arguments.length - 1];\n" +
                    "var el = document.querySelector('html');\n" +
                    "if (!window.angular) {\n" +
                    "    callback('false')\n" +
                    "}\n" +
                    "if (angular.getTestability) {\n" +
                    "    angular.getTestability(el).whenStable(function(){callback('true')});\n" +
                    "} else {\n" +
                    "    if (!angular.element(el).injector()) {\n" +
                    "        callback('false')\n" +
                    "    }\n" +
                    "    var browser = angular.element(el).injector().get('$browser');\n" +
                    "    browser.notifyWhenNoOutstandingRequests(function(){callback('true')});\n" +
                    "}";

            JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
            String isProcessingFinished = javascriptExecutor.executeAsyncScript(hasAngularFinishedScript).toString();

            return Boolean.valueOf(isProcessingFinished);
        }
    };
}
public boolean waitForJquery(WebDriver driver, int timeout) {
    return waitFor(driver, "return jQuery.active;", "0", timeout);
}

public boolean waitFor(WebDriver driver, final String javascriptString, final String targetString, int timeout) {
  WebDriverWait wait = new WebDriverWait(driver, timeout, 500L);

  /*
   * If you are curious about what follows see:
\  *    http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/ExpectedCondition.html
   * 
   * We are creating an anonymous class that inherits from ExpectedCondition and then implements interface
   * method apply(...)
   */
   ExpectedCondition<Boolean> isLoaded = new ExpectedCondition<Boolean>() {

      public Boolean apply(WebDriver driver) {
        String jsReturnedValue = "";

        try {
            jsReturnedValue = String.valueOf(((JavascriptExecutor)driver).executeScript(javascriptString));
            return (jsReturnedValue.equals(targetString));
        } catch (Exception e) {
          log.info("Looking for: " + javascriptString + ", e.message: " + e.getMessage());
          return true;  // If Javascript not found then don't wait for it
        }
      }
    }; // Terminates statement started by ExpectedCondition<Boolean> isLoaded = ...

  return wait.until(isLoaded);
}
public boolean waitForPrototypeAjax(WebDriver driver, int timeout) {
    return waitFor(driver, "return Ajax.activeRequestCount;", "0", timeout);
}