【问题标题】:If Selenium driver does not find Element ( If - else if) move on, yet it throws a can't find element error如果 Selenium 驱动程序没有找到元素(如果 - 否则如果)继续前进,但它会引发找不到元素错误
【发布时间】:2019-10-25 21:43:01
【问题描述】:

长话短说 - 我有一个没有 ID 并且有一个复合类的按钮(所以 selenium 讨厌它/找不到它)。所以我使用 XPath 选择器,效果很好

driver.findElement(By.xpath("//input[@value='Continue to Payment']")).click()

但按钮会根据所使用的语言而变化。

所以现在,我有

if (driver.findElement(By.xpath("//input[@value='Continue to Payment']")).isDisplayed()){ 
           driver.findElement(By.xpath("//input[@value='Continue to Payment']")).click();
    }

else if (driver.findElement(By.xpath("//input[@value='Paiement']")).isDisplayed()){           
        driver.findElement(By.xpath("//input[@value='Paiement']")).click();
    }

else if ( same thing as above but for another language)

但是当 Selenium 在通过第一个 if 语句后出错时:

no such element: Unable to locate element:{"method":"xpath","selector":"//a[contains(text(),'Checkout')]"}

我知道该元素不存在.. 所以我不希望它做任何事情并继续下一个 if else 语句。 我在这里想念什么?

【问题讨论】:

  • 我认为你错过了 try/catch。您也可以使用 findElements 来获取列表。如果列表大小>0,您已经找到它.... 类似这样的内容:wait = new WebDriverWait(driver, ec_Timeout); List element = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(your_xpath))); if (element.size() > 0) {....} (但你还是应该使用 try/catch...)等待会抛出超时...
  • 分享页面的HTML,也许还有其他方法可以定位输入。
  • @pcalkins 谢谢!!这非常适合转移到第二个值!可以说它既不是第一个也不是第二个。并且 catch 也会有一个例外。我会尝试 {code A - not there} catch{ other code - not there} Try {code c} or Try{ code A +Code B] Catch{Code C}
  • 想到的一件事是提前初始化 List。如果最后列表大小> 0,则单击第一个元素...(在找到或未找到所有元素之后...)我想您还可以创建一个函数,如果一个项目返回列表被发现。如果已经找到,这将避免检查更多语言。
  • 请尝试/捕获每个单独的 findElements() 调用...此外,如果元素不是通过 javascript 创建的,则不需要 webdriverwait。

标签: java selenium chromium


【解决方案1】:
try {
            if (driver.findElement(By.xpath("//input[@value='Continue to Payment']")).isDisplayed()){
                driver.findElement(By.xpath("//input[@value='Continue to Payment']")).click();
            }

            else if (driver.findElement(By.xpath("//input[@value='Paiement']")).isDisplayed()){
                driver.findElement(By.xpath("//input[@value='Paiement']")).click();
            }
            else
                System.out.println("Button not found");

        } catch(NoSuchElementException | StaleElementReferenceException e) {
            System.out.println("Impossible to click the pop-up. Reason: " + e.toString());
        }

尝试上述解决方案,希望它对您有用。在您的示例中,为 else if (driver.findElement(By.xpath("//input[@value='Paiement']")).isDisplayed) 编写了错误代码。

【讨论】:

    【解决方案2】:

    您可以使用单独的简短方法来实现预期结果并记录错误。

    public WebElement getElement(WebDriver driver, String XPATH, int timeoutInSeconds){
        WebElement elem = null;
        try{
            WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
            elem = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(XPATH));
        } catch (Exception e){
            // log or print error.
        }
        return elem;
    }
    

    你可以这样称呼它

    WebElement e = getElement(driver, "//input[@value='Continue to Payment']", 10); 
    if (e != null) {
        e.click();
    } else {
      e = getElement(driver, "//input[@value='Paiement']", 5);
      if (e != null) {
        e.click();
      } /// and so on....
    }
    

    这样您可以调整每个元素的等待时间,并且如果由于语言而缺少任何一个元素,也不会陷入错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-19
      • 2018-08-26
      • 1970-01-01
      相关资源
      最近更新 更多