Java 等待元素的可见性

Java 等待元素的可见性,java,selenium,xpath,Java,Selenium,Xpath,使用Java和Selenium 我们有一个waitForXPathVisibility(xpath)方法,它最终会调用wait.UntilexExpectedConditions。。。元素的可见性。这种方法效果很好。我只是在这里缩写了一下。所以请告诉我waitForXpathVisibility(xpath)将一直等到它可见 所以我们有一页医学专科。它最初只是用英语写的。因此,如果您搜索“neuro”,HTML将类似于 <ul id="ui-id-1" tabindex="0" class

使用Java和Selenium

我们有一个waitForXPathVisibility(xpath)方法,它最终会调用wait.UntilexExpectedConditions。。。元素的可见性。这种方法效果很好。我只是在这里缩写了一下。所以请告诉我waitForXpathVisibility(xpath)将一直等到它可见

所以我们有一页医学专科。它最初只是用英语写的。因此,如果您搜索“neuro”,HTML将类似于

<ul id="ui-id-1" tabindex="0" class="ui-menu ui-widget ui-widget-content ui-autocomplete ui-front" style="width: 476.672px; position: relative; top: -1005px; left: 542.828px; display: none;">
 <LI class="ui-menu-item"> Neurology</Li>
 <LI class="ui-menu-item"> Neuro Surgeon </li>
 </UL>
它会回来的。但是搜索总是找到第一个,因此上面的xpath将指向第一个英文的,它将不再可见,因此将超时

是否有办法等待任何匹配xpath的元素可见?请注意,AllElements()的可见性将不起作用,因为它们不会全部可见


我想可以做的是预先获得带有@id和“width”作为样式一部分的所有元素的计数。“显示;无”将不起作用,因为有其他具有此值的。然后进行搜索,等待s的数量增加1,获取最后一个的id,然后等待其下方的
  • 变为可见,但这似乎有点过分。应该有办法等待一个可见的
  • ?然后我可以返回它的父级

    使用
    JavaScript
    executor来获取隐藏值。如果您想获得Neurologa的值,请尝试下面的代码。希望这有帮助

    WebElement element=driver.findElement(By.xpath("(//ul[@id='ui-id-2']/li[@class='ui-menu-item'])[1]"));
    JavascriptExecutor js = (JavascriptExecutor) driver;
    String output=(String) js.executeScript("return arguments[0].innerHTML;", element);
    System.out.println(output);
    


    我想你要找的是

    //ul[contains(@class,'ui-menu')][last()]/li
    ^ finds a UL
        ^ that contains the 'ui-menu' class
                                    ^ get the last one (the one that contains visible elements)
                                            ^ get the LI children
    
    您应该能够使用组件的可见性来等待所有这些组件都可见


    旁注。。。不要编写仅限于XPath的方法
    waitForXPathVisibility()
    。这使得您必须为使用的每种定位器类型(CSS选择器、名称、id等)编写一个方法。取而代之的是将
    By
    作为参数,并将其设置为泛型。见下文

    public void waitForVisibilityOfElement(By locator)
    {
        new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(locator));
    }
    
    您还可以为多个元素编写一个

    public void waitForVisibilityOfElements(By locator)
    {
        new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator));
    }
    
    你用起来就像

    By someElementLocator = By.id("someId");
    By someElementLocator = By.xpath("//ul[contains(@class,'ui-menu')][last()]/li"); // or this
    By someElementLocator = By.cssSelector("ul.ui-menu"); // or this
    waitForVisibilityOfElement(someElementLocator);
    

    URL是公开的吗?是的,但不确定这是否重要?哦,我明白了。你想看看吗?是的,这是公开的。转到,然后“查找提供商”。做免责声明,然后用英语和西班牙语搜索(任意次数)。但我不知道ul的实际id。在这种情况下,它是ui-id-2,但可以是随机的OK。在这种情况下,您可以通过文本找到元素。是您吗want@Tony:检查或选项。可以吗?如果没有,请在你原来的帖子中详细说明。我喜欢[最后一个]的想法
    public void waitForVisibilityOfElement(By locator)
    {
        new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(locator));
    }
    
    public void waitForVisibilityOfElements(By locator)
    {
        new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator));
    }
    
    By someElementLocator = By.id("someId");
    By someElementLocator = By.xpath("//ul[contains(@class,'ui-menu')][last()]/li"); // or this
    By someElementLocator = By.cssSelector("ul.ui-menu"); // or this
    waitForVisibilityOfElement(someElementLocator);