繁体   English   中英

Selenium WebDriver。 从div列表中选择元素

[英]Selenium WebDriver. Select element from div list

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>  

因此,我需要从组之一中选择所需的元素(应该不处于活动状态,确定)。
当我选择一个组时,什么也没有发生,没有错误,并且看不到所选组打开。 即使片刻。
但是,当我尝试在先前单击的组中选择一个元素时,会收到

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();  

单击组或将其悬停时,它的显示方式如下:
从代码中可以看到,选定/打开的组将接收“组可见”类参数。

单击组或将其悬停在组上时,它看起来如下:

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

// 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();

您也可以尝试使用JavascriptExecutor单击

WebElement element= driver.findElement(By.xpath("Your Xpath"));

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);

希望它能对您有所帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM