【问题标题】:org.openqa.selenium.ElementNotVisibleException: element not visibleorg.openqa.selenium.ElementNotVisibleException:元素不可见
【发布时间】:2016-12-23 11:57:10
【问题描述】:

当我运行测试时我的测试失败了,我再次运行测试,测试通过,代码没有改变但结果不同。为什么? 这是我的代码:

    public ProductPage clickOnAccessories(){

        //Click on link “Accessories”
        waitForElementToBeDisplayed(driver.findElement(By.xpath("//a[text()='Product Category']")), 200);
        Actions act = new Actions(driver);
        act.moveToElement(driver.findElement(By.xpath("//a[text()='Product Category']")));
        act.perform();

        link_Accessories.click();

        //Expected: Page “Accessories” has been opened with correct url, headline, tab title
        Assert.assertTrue(driver.getCurrentUrl().equals("http://store.demoqa.com/products-page/product-category/accessories/"));
        Assert.assertTrue(driver.getTitle().contains("Accessories"));
        Assert.assertTrue(tab_title_Accessories.getText().contains("customers"));

        return PageFactory.initElements(getDriver(), ProductPage.class);
    }


public void waitForElementToBeDisplayed(final WebElement element, int defaultWaitTime) {
    for (int i = 500; i < defaultWaitTime; i += 100) {
        Utils.waitTime(500);
        if (isElementDisplayed(element)) { return; }
    }
}

protected boolean isElementDisplayed(final WebElement element) {
    boolean isDisplayed = false;
    try {
        final String tagName = element.getTagName();
        if (!tagName.isEmpty()) { isDisplayed = element.isDisplayed(); }
        return isDisplayed;
    } catch (final Exception e) { }
    return isDisplayed;
} 

这是我的错误:

org.openqa.selenium.ElementNotVisibleException: element not visible
  (Session info: chrome=55.0.2883.87)
  (Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 6.3.9600 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 51 milliseconds
Build info: version: 'unknown', revision: 'c7b525d', time: '2016-09-01 14:57:44 -0700'
System info: host: 'Gaga', ip: '192.168.1.6', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_101'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed), userDataDir=C:\Users\Dragana\AppData\Local\Temp\scoped_dir10100_17794}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=55.0.2883.87, platform=WIN8_1, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: a24c7146c1accfdd3ef521786f4cb399

【问题讨论】:

    标签: java selenium selenium-webdriver pageobjects


    【解决方案1】:

    您可能需要使用嵌入式方法等到目标元素变得可见:

    // Set your own value of timeoutInSeconds
    WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()='Product Category']")));
    

    【讨论】:

    • 我现在的错误:org.openqa.selenium.TimeoutException:预期条件失败:等待 By.linkText 定位的元素的可见性:iMacs(以 500 MILLISECONDS 间隔尝试 5 秒)
    • 这是您点击Product Category 后的下一步,对吧?试试wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(text(), 'iMacs')]")));
    • 我试过了:wait.until(ExpectedConditions.visibilityOfElementLocated(By.‌​xpath("//a[contains(‌​text(), 'iMacs')]"))); wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));
    • waitForElementToBeDisplayed(driver.findElement(By.linkText("‌​iMacs")), 200);我的测试有时有效,有时即使我尝试了所有这些也不起作用
    【解决方案2】:

    您必须尝试 Webdriver 等待以消除此错误。输入要触发等待的 xpath 表达式。

    WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("<Enter your xpath expression here>")));
    

    【讨论】:

    • 我现在的错误:org.openqa.selenium.TimeoutException:预期条件失败:等待元素可点击:By.xpath://a[text()='Accessories'](尝试30 秒,间隔 500 毫秒)
    • 如果您能提供您想要点击的前端的 HTML 代码和屏幕截图,将会很有帮助。在您的 HTML 中,请同时检查 iframe。
    猜你喜欢
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 2014-11-13
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多