【问题标题】:Cannot find element using Selenium Webdriver使用 Selenium Webdriver 找不到元素
【发布时间】:2018-05-15 23:48:38
【问题描述】:

我正在尝试导航到“https://developers.google.com/”。

然后我单击 xpath 的链接并重定向到另一个页面。

只要找到元素并点击第一页上的链接,一切都会起作用。

但是在我进入新页面后,我似乎找不到想要查找的元素。

重定向页面是“https://cloud.withgoogle.com/next18/sf/?utm_source=devsite&utm_medium=hpp&utm_campaign=cloudnext_april18

这是检查文本是否相等。

   confirmText("Imagine", "//*[@id=\"main\"]/span/div[2]/div/div/div[1]/div[1]/div/div[1]/div[2]/h3");

    public static void confirmText(String text, String xpath) {
    System.out.println("Trying to confirm that given string is equal to the text on page.");
    WebElement element = driver.findElement(By.xpath(xpath));
    System.out.println("Test case: " + text);
    System.out.println("Result: " + element.getText());
    if (element.getText() == text) {
        System.out.println("\nEquals.");
    }
    else {
        System.out.println("\nDoes not equals.");
    }
    System.out.println("\n\n");
}

这是发送密钥。

    public static void sendKeys() {
    WebElement firstname = driver.findElement(By.id("firstName"));
    firstname.sendKeys("John");
    WebElement lastname = driver.findElement(By.xpath("//*[@id=\"lastName\"]"));
    lastname.sendKeys("Doe");
    WebElement email = driver.findElement(By.xpath("//*[@id=\"email\"]"));
    email.sendKeys("johndoe@gmail.com");
    WebElement jobtitle = driver.findElement(By.xpath("//*[@id=\"jobTitle\"]"));
    jobtitle.sendKeys("Software Engineer");
    WebElement company = driver.findElement(By.xpath("//*[@id=\"company\"]"));
    company.sendKeys("ABCD");
}

错误是

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="main"]/span/div[2]/div/div/div[1]/div[1]/div/div[1]/div[2]/h3"}

【问题讨论】:

  • 在你的驱动声明之后,立即添加这一行driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);它会为你工作。
  • 然后我点击 xpath 的链接?您尝试点击哪个链接,请提供手动步骤。

标签: java selenium selenium-webdriver webdriver automated-tests


【解决方案1】:

在脚本开始之前,实际上只有第一页等待完成加载。如果您离开页面,则需要手动等待。无需等待,脚本将在单击更改页面的链接后立即继续执行下一个命令,并且由于页面尚未加载,您将找不到该元素。这给出了错误。

最简单的等待方法是使用“WebDriverWait”

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(>someid>)));

这里有更简单的例子: http://toolsqa.com/selenium-webdriver/wait-commands/

【讨论】:

    【解决方案2】:

    您可以使用 for Imagine 尝试这个 xpath,然后像 @chris 提到的那样等待

    //div//h3[contains(text(),'Imagine')]
    

    【讨论】:

      【解决方案3】:

      JavascriptExecutor 可以用来获取元素的值,

      参考代码,

      confirmText("Imagine", "//*[@id=\"main\"]/span/div[2]/div/div/div[1]/div[1]/div/div[1]/div[2]/h3");
      
      public static void confirmText(String text, String xpath) {
      System.out.println("Trying to confirm that given string is equal to the text on page.");
      WebElement element = driver.findElement(By.xpath(xpath));
      JavascriptExecutor executor = (JavascriptExecutor)driver; 
      System.out.println("Test case: " + text);
      System.out.println("Result: " + executor.executeScript("return arguments[0].innerHTML;", element););
      if (text.equals(executor.executeScript("return arguments[0].innerHTML;", element))) {
          System.out.println("\nEquals.");
      }
      else {
          System.out.println("\nDoes not equals.");
      }
      System.out.println("\n\n");}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-11
        • 2016-09-07
        • 2017-10-24
        • 1970-01-01
        • 1970-01-01
        • 2016-04-17
        • 1970-01-01
        相关资源
        最近更新 更多