无法定位元素 - 使用selenium webdriver在不同的语言环境中自动化网站

问题描述 投票:0回答:1

我正在尝试自动化具有不同度量系统的网站。该应用程序支持美国公制系统和英制公制系统。例如:如果我从美国服务器打开应用程序,我会看到英尺和英寸的文本框,如果我从不同的服务器打开相同的应用程序,我会看到Centimeters中的文本框。

我编写了我的代码,以便它首先检查是否存在带有英尺和英寸的文本框元素,如果它存在然后它继续并在这些文本框中输入值,否则如果不存在英尺和英寸,则输入Centimeters文本框中的值。

/*
 *
 * This block is for the values in US version
 *
 */
@FindBy(xpath = ".//*[@id='profile_height_large_value']")
WebElement CurrentHeightinFeet;

@FindBy(xpath = ".//*[@id='profile_height_small_value']")
WebElement CurrentHeightinInches;

/*
 * This block is for the values in British version
 *
 */

@FindBy(xpath = ".//*[@id='profile_height_display_value']")
WebElement CurrentHeightinCM;

我检查它是否在任何一个版本中的代码如下所示。

public void userFitnessDetails() {
    CurrentWeight.sendKeys("70");

    if (CurrentHeightinFeet.isDisplayed()) {
        CurrentHeightinFeet.clear();
        CurrentHeightinFeet.sendKeys("5");
        CurrentHeightinInches.clear();
        CurrentHeightinInches.sendKeys("10");
    }

    CurrentHeightinCM.clear();
    CurrentHeightinCM.sendKeys("170");
}

如果我执行上面的代码我得到一个错误 - 失败:signUp org.openqa.selenium.NoSuchElementException:无法找到元素:

{"method":"xpath","selector":".//*[@id='profile_height_large_value']"}

有人可以指导我纠正这个吗?

谢谢

java selenium xpath pageobjects
1个回答
1
投票

假设 - 您正在使用英国服务器并遇到美国脚长输入问题。页面已完全加载且元素完全可用,因此不存在等待问题。

你会遇到isDisplayed()方法的问题。由于样式属性显示设置,它用于确定页面中存在的元素是否不可见,反之亦然。在这里,您根据服务器位置提到了相关的html元素是否存在。如果元素不可用,那么您将看到异常。

您可以使用更安全的driver.findElements()代替isDiplayed()条件检查,它会返回一个列表,您可以检查大小以确定可用性。

© www.soinside.com 2019 - 2024. All rights reserved.