java - Selenium 网络驱动程序。从 div 列表中选择元素

标签 java select selenium-webdriver

我的 HTML 页面上有以下区域:

<div class="t2-selector">  
    <div class="">  
        USA  
        <div>  
            <div>  
                <div>  
                    <div class="selected" asset-id="129">Google</div>  
                    <div asset-id="130">Microsoft</div>  
                    <div asset-id="126">Apple</div>  
                </div>  
           </div>  
       </div>  
</div>  
<div class="inactive">  
    Europe  
    <div>  
        <div>  
            <div>  
                <div class="inactive" asset-id="127">BT</div>  
            </div>  
        </div>  
   </div>  
</div>  
<div class="">  
    Currencies  
    <div>  
        <div>  
            <div>  
                <div asset-id="135">EUR/USD</div>  
                    <div asset-id="136" class="">GBP/USD</div>  
                    <div asset-id="137" class="">USD/JPY</div>  
                    <div asset-id="138" class="selected">USD/CHF</div>  
                    <div asset-id="139">AUD/USD</div>  
                    <div asset-id="140">USD/CAD</div>  
                </div>  
            </div>  
       </div>  
</div>  

所以我需要从其中一个组中选择所需的元素(不应该处于非 Activity 状态,好吧)。
当我选择一个组时,没有任何反应,没有错误,并且我没有看到所选组打开。 即使是片刻。
但是当我尝试选择先前单击的组中的元素时,我收到

org.openqa.selenium.ElementNotVisibleException: element not visible  

错误。
所以我知道,在我单击所需元素的那一刻,它是不可见的,因为该组未显示为打开。
但为什么呢?
我该如何解决这个问题?
目前我正在使用以下代码:

String selectedGroup = getValue("group",'o');  
    String xpath1 = "//div[contains(text(),'" + selectedGroup + "')]";  
    driver.findElement(By.xpath(xpath1)).click();  
    webElement = driver.findElement(By.xpath(xpath1));  
    String className = webElement.getAttribute("class");  
    if(className.contentEquals("inactive"))  
        throw new ElementInactiveException("Selected group appears inactive. Exiting the test");  

    String optionAssetID = getValue("assetID",'o');  
    String xpath2 ="//div[@asset-id='" + optionAssetID + "']";  

    driver.findElement(By.xpath(xpath2)).click();  

错误发生在以下行:

driver.findElement(By.xpath(xpath2)).click();  

当单击一个组或将鼠标悬停在其上时,它看起来如下:
正如您从代码中看到的,所选/打开的组接收“组可见”类参数。

When clicked on a Group or hovered over it it looks so:

最佳答案

您可以将鼠标悬停在下拉菜单上将其打开,然后单击您的元素

// simulate mouse movement to the dropdown 
Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.xpath(xpath1))).perform();

// wait for the element to be visible before clicking on it
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(xpath2)).click();

关于java - Selenium 网络驱动程序。从 div 列表中选择元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35276104/

相关文章:

java - 使用 JNDI 的 Marklogic XCC ContentSource

php - 展示产品或系列

python - 将 SameSite Chrome 标志传递到 Selenium 远程服务器

node.js - 语法错误 : Use of const in strict mode?

Java EE 容器不允许使用 Java SE 并发 API

java - 组织.hsqldb.HsqlException : data exception: invalid character value for cast

MySQL 使用 COUNT 扩展 SELECT 查询

java.lang.ClassCastException : org. openqa.selenium.By$ById 无法转换为 org.openqa.selenium.WebElement

Java - Objective C char UTF-8 问题

mysql - 多行的 SQL 条件