我在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>
所以我需要从其中一个组中选择所需的元素(不应该处于非活动状态,OK)
当我选择一个组时没有任何事情发生,没有错误,我没有看到所选组打开。
即使是片刻。
但是,当我尝试在之前点击的组中选择一个元素时,我会收到
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();
单击某个组或将鼠标悬停在该组上时,它会以下列方式查看:
从代码中可以看出,所选/打开的组接收“group-visible”类参数。
答案 0 :(得分:2)
您可以将鼠标悬停在下拉列表中以打开它,然后点击您的元素
// 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();
答案 1 :(得分:1)
您也可以尝试点击使用JavascriptExecutor
WebElement element= driver.findElement(By.xpath("Your Xpath"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
希望它会对你有所帮助:)。